game loop fix and refactoring