change from depth first logic to breadth first