Fixed race conditions. Now code is a ball of hacks. A working ball of hacks though.