Modified to include middle 2, as well as changes for (i1=i2) if i3 and/or i4 are
null