More robust error handling (revert working environment on error)