Edited logic to prepare for integration with UndoManager