Modified loop counters. Working version

Needs cleanup before release.