                     else if moveScore == bestScore {
                         bestMoves.append((move, movePV.prepending(move)))
                     }
-                    group.leave()
                 }
             }
         }

                    else if moveScore == bestScore {
                        bestMoves.append((move, movePV.prepending(move)))
                    }
                    group.leave()
                }
            }
        }
    case .black:
        bestScore = Double.infinity
        for move in moves {
            group.enter()
            DispatchQueue.global().async {
                var newPosition = position.after(move)
                let (moveScore, movePV) = minimaxSearch(position: &newPosition,
                                                        depth: searchDepth - 1,

    case .white:
        bestScore = -Double.infinity
        for move in moves {
            DispatchQueue.global().async(group: group) {
                var newPosition = position.after(move)
                let (moveScore, movePV) = minimaxSearch(position: &newPosition,
                                                        depth: searchDepth - 1,
