this method gets a little complicated.  i would prefer to see better variable names (i,j,k) to understand what's going on