remade endGame function