finished rewriting segmentation