change the way to detect loops