prevent endless loop

A minor fix in order not to end up in an endless loop in some cases.