Segmentation algorithm