Hrm, should we write this before we load the segment?  Also, should we have a check for if the file already exists, or just always overwrite like this code is doing?