Fixed bugs and added endgame support