Undo / redo functionality. Some fixes here and there