added bfs for supporting nested element hierarchies