improve iterateChildren function