small improvement to bfs