refact minimum distance and average distance