Big refactor of undo/redo.