Refactored game loop