improved the performance of the getMinimumDistance function