Fixes BFS infinite loop bug