updated optimal_move method, refactored