Fix an issue where extra stuff would be written to the end of files