Fix bug so that temp files are properly removed