made it add nodes to visited when spotted, to avoid loops