refactor game loop