Fixed a bug and improved cleanup of files