Try this code and adapt to your needs.