Finished the segmentation algorithm.