Undo - fix some bugs, write a test