moved tempfile writing out of main