properly close tempfile