replaced pointer dists for max_distance with this