Defining "doesn't work" would be nice. Knowing your use case means we can help you more accurately and not just provide broad solutions.
Also, I don't know if this was from cropping out code or whatever, but you're missing an end to close that if statement.