Added functions to calculate distances