Optimized to avoid repeated traversals