Limit undo stack to ten entries

Also some cleanups.