That's quite a bit of code to debug, I'd suggest you provide an SSCCE that demonstrates the problem in a much more bite-sized program.

Without the SSCCE, I'm only guessing, but I would bet you're...