improved DistancetoPrimitive function