Fix some double-reversed i-j problems in this code