bfs bug fix + logic changed a little