redesigned BFS implementation