fixes and refactorings for undo