I have the feeling that your real problem lies in part of the code that you aren't showing.
If this is true, don't feel bad as it happens to the best of us.

If you would post your full code here, or in a Pastebin and then reference the pastebin here, I should be able to determine if what I suspect is correct, and then be of more assistance.

Please note that I have company coming for the day in a couple of hours, so if you don't get it to me by then, I won't be able to work on it until Monday.