changed min_dist from final to normal variable, now distance is read at
the beginning.