polishing up traversal function