Added selection management and undo handling