changed  if statement to while for distance  above 500m