Added missing bounds check in loop.