refactored game loop