added endgame logic