bugfix: not to close a file already moved
bugfix: close tmp file before renaming it