Refactored minimax algorithm