refactoring the game loop