Modified the loop indexes for better understanding.

Also, added necessary comments.