fix loop function. if customized distance.
ver++