Function to keep track of iteration count