bfs refactor