added a lot more checking/cleaning up things when --tmpdir is specified