main game logic rewritten