Refactored a bit and added tests for branching undo