Merged in some of the better elements of the temp_files version