refactor getAncestorsAndSelf method