ver 3 of segmentation algo -- working