Fixed undo/redo. Other small refactoring.