When posting code, please use the highlight tags to preserve formatting. You can edit your post and add them now.

Have you stepped through this with a debugger, or at least added print statements,...