refactr bfs branch