Work on rtb piecewise distance and some other rework