Moved main gameplay loop to its own function