Fixed distance calculation for min/max bounds, this improved speed. Unified c# and javascript implementation.