BFS functional, needs improvement