Implemented calculation of distance