Please help me understand this behavior

You post nearly 800 lines of poorly-structured code with not a single meaningful comment, and no trace or debug prints, so I suspect you're probably right.

Good luck.