changed bfs implementation