refactored endgame method