Refactored undo and redo methods