refactor undo and redo functions