implemented speed up for segmentation step