Updated Segmentation Function and Clean-up