some refactoring, basic endgame