Rewrite the tree cursor in a saner way