finish main_segmentation (to be cleaned and commented)