Generic traversal function.