added cleanup of temp files on errors