close handles as soon as possible
stop using temporary files, preserve permissions