added a counter to debug an infinite loop