finished bfs function