alternative bfs code