checkAncestry procedure implementation