First implementation of BFS