Changed from depth-first to breadth-first search for better performance and to get rid of the kludgy depth parameter