avoid a few if(x==0) checks in the inner loops