Cleaned up and fixed some bugs in segment stitching