Updated logic for traversing up the ancestral tree.