Fix bug in leave detection in cases in which self-loops are defined + doc.