Added better checking for the end of a game.