And this part of code works just fine.
The issue is that another, non-related part of code is causing problems.
Why changing value of certain variable, which is not used at all, causes issues with another variable?
Ioannis gets it. You don't. That code doesn't "work just fine".

Your array index statements are screwed up, so you're accessing who knows what in memory.
That would explain the mysterious "I add unrelated code and things change".

There are any number of places for all that to go south... I don't trust many of those array accesses to be valid.