       || (x <= xmid && y <= ymid && z >= zmid)
       || (x >= xmid && y >= ymid && z >= zmid)
       || (x <= xmid && y >= ymid && z <= zmid);
-    const char bits = (bit0 << 2) | (bit1 << 1) | (bit2 << 0);
+    char bits = (bit0 << 2) | (bit1 << 1) | (bit2 << 0);
+    bits = codeindex[bits]; // rotate
     code = code | bits;
+
 
     xmin = NEW_DIMENSION_MIN(x, xmid, xmin, xmax);
     xmax = NEW_DIMENSION_MAX(x, xmid, xmin, xmax);

    ymax = NEW_DIMENSION_MAX(y, ymid, ymin, ymax);
    zmin = NEW_DIMENSION_MIN(z, zmid, zmin, zmax);
    zmax = NEW_DIMENSION_MAX(z, zmid, zmin, zmax);
  }
  codes[i] = code;
}

  const double y = star.y;
  const double z = star.z;

  // rotations for recursive children.
  const short codeindex_rotations[8][8] = { {0, 7, 6, 1, 2, 5, 4, 3},
                                            {0, 1, 6, 7, 4, 5, 2, 3},
                                            {0, 1, 2, 3, 4, 5, 6, 7},
                                            {6, 1, 2, 5, 4, 3, 0, 7},
                                            {0, 7, 6, 1, 2, 5, 4, 3},
                                            {0, 1, 2, 3, 4, 5, 6, 7},
                                            {4, 5, 2, 3, 0, 1, 6, 7},
                                            {6, 1, 2, 5, 4, 3, 0, 7},
  };

  // Used to rotate the recursive curves, so they align to form one long curve.
  short codeindex[] = {0, 1, 2, 3, 4, 5, 6, 7};

  uint64_t code = 0;
  #pragma unroll
  for(int i = 0; i != 21; ++i) {
    code = code << 3;

    const double xmid = GET_MID(xmin, xmax);
