BFS implementation.