dist_array is now calculated only once