Small improvements

Error code is always 1
Use mktemp for temporary directories
Better error handling
Don't remove existing directory files