Correcting logic error in implementing loop.