Fixed a bug of index counter in for loop.