Edited logic to integrate with functions in UndoManager