refactor to share more code with restore