Fixing better error handling on tempfile