Fixes and tidy up

Ensures /tmp (or $TMPDIR) is used for the temporary directory, rather
than the current directory (which may not be accessible after switching
user).