added file writes, updated with correct handling of newline characters at the end of the concatenated in files