Code cleanup for undo