fix bfs algorithm