first version of segmentation algorithm