My take on breadth-first search algorithm