Refactor end-game logic