accomplish the bfs function