adding code to save distance matrix