Corrected some nasty bugs in the rhythm detection; prevents infinite loops and might even improve rhythm detection in some border cases