Changed how BFS is calculated