Refactoring minimax algorithm