Fix logic for loopless graphs