Refactor minimax function