More supporting logic for depth sort