Fixed bugs in main game loop