finished flattening of 3d loop (untested)