Cleaned up undo/redo a bit: the selection is now preserved when using
undo/redo, and there aren't as many situations where there are
duplicates on the stacks. There are still some issues with undoing all
the way to the start.