added a counter to keep track of the last cell processed