Fix the infinite loop which occurs when rows wrapped