private GridSquare getOptimalSquareHelper(List<DistanceSquarePair> squares, Location loc, boolean closest){
        int bestDist;
        GridSquare bestSquare = null;

        if (closest) bestDist = Integer.MAX_VALUE;
        else bestDist = -1;
        
        for (DistanceSquarePair pair : squares) {
            int dist = distance(pair.gridSquare.getLocation(), loc);
            if (closest) {
                if (dist < bestDist && pair.gridSquare.getAnimal() == null) {
                    bestDist = dist;
                    bestSquare = pair.gridSquare;
                }
            } else {
                if (dist > bestDist && pair.gridSquare.getAnimal() == null) {
                    bestDist = dist;
                    bestSquare = pair.gridSquare;
                }
            }
        }

        return bestSquare;
    }