Refactored the SegmentBySegment.py main function