Refactor #tmp_directory into helper module

Similar to #1126, This is the result of some exploratory work that @vipulnsward and I did on Sunday.

We opted to move the prefix to an argument, although the prefix itself is probably unnecessary if we added milliseconds to the filename to prevent possible conflicts.

Again, the work was done so the PR was opened. Would love to get feedback on this one.