Fix bug in BFS algorithm