Refactor tempdir management