Factored out common traversal code