refactored looping to allow better optimization
added bounds for (k) as function of best-known-distance for more optimization