I FIXED IT!
I've been working on this for 6 months and just could not find the bug.
It was - as I've been expecting for quite some time now - an array that was being addressed with a wrong index (resulting in it setting some other part of the memory). By writing a script that added a line before all wait calls, I found out which function it was in. There was some troubles with proc_mode, as those calls changed my results, but I finally found it.
Thanks a lot for all your help!