Add error handling to DistanceMatrix