Minor i,j swaps to make the code easier to understand.