updated isAncestor function