Fixed corner checking and renamed the i, j, ii, jj values to sy, sx, py, px respectively