fix distance calc loop index