Refactor and cleanup storage and shutdown code.