Fixed a whole bunch of bugs around error handling and copying things to and from the tmp directory.