Slight refactor of BFS