unpack-image: better cleanup and error handling