Added better error handling around temporary files.