Refactor the segmentation tool backend functions