Added proper error checking during loop.