rewrite bfs, add tests