BFS refactored