Implemented andrey's modified bfs