total refactor of segmentation