refactored main game logic