Added weighting with the maximum of the distances