Refactor game loop