Fixed game loop to properly handle game restarting and ending