corrected error handling in ...count_distances