Refactor save/restore