BFS with debug code