update temp file writing to be safer