Fixed a file clean up bug on error.