moved game logic into its own function