Pipes API, walk pipe segments only once, optimization + pipe replacement fix