added the code to write segment files