made bfs code faster