Cleaned up endgame detection code.