Finish distance matrix function