Working BFS implementation.