Added custom loop for distance checking