functional BFS