Better treatment of temporary file
Ensure temporary files are cleaned up on exit