implemented distance calculation