Fixed a bug with a loop counter