Fix cycle-finding bug