better management of errors and deleting temporary files