Update: code compactification; modified ranges: split work with distance.