First working version of bfs.