refine the BFS