prevent endless loops which causes high CPU usage

As reported in issue 176