So the first option worked? If so, there's no need for a "best way". If your code has some issues you'll have to be more specific, rather than hope someone provides a solution actually fits your needs without knowing them. Or if you want advice about your code, you'll have to post it.
Edited: in hindsight my previous formulation sounded harsher than intended.