         {3,2}, // 2
         {3,0}, // 3
         {4,5}, // 4
-        {5,6}, // 5
-        {6,7}, // 6
+        {6,5}, // 5
+        {7,6}, // 6
         {7,4}, // 7
         {0,4}, // 8
         {1,5}, // 9
         {2,6}, // 10
         {3,7}  // 11
       };
-      mctable[16 * n + i] = (vv[e[t][0]] << 3) | vv[e[t][1]];
+
+      const auto a = vv[e[t][0]];
+      const auto b = vv[e[t][1]];
+      assert(((~a & b) | a) == b);
+
+      mctable[16 * n + i] = (vv[e[t][0]] << 3) |  (~vv[e[t][0]] & vv[e[t][1]]);
       if (i % 3 == 2) {
         std::swap(mctable[16 * n + i - 1], mctable[16 * n + i]);
       }
     }
     counts[n] = i;
   }
+
+#if 1
+  for (size_t j = 0; j < 256; j++) {
+    if (j % 16 == 0) {
+      fprintf(stderr, "\n");
+    }
+    fprintf(stderr, "%d, ", counts[j]);
+  }
+  fprintf(stderr, "\n");
+  fprintf(stderr, "\n");
+#endif
+#if 0
+  for (size_t j = 0; j < 256; j++) {
+    fprintf(stderr, "    ");
+    for (size_t i = 0; i < 16; i++) {
+      const auto t = mctable[16 * j + i];
+      if (t == 0xffu) {
+        fprintf(stderr, "0, ");
+      }
+      else {
+        fprintf(stderr, "0%02o, ", t);
+      }
+    }
+    fprintf(stderr, "\n");
+  }
+#endif
 
   // reference code
   fprintf(stderr, "%d %zu\n", nx * ny * nz * 2, scalarField_host.size());

using namespace ComputeStuff::MC;

namespace {

  // Table from 'Marching Cubes Example Program` by Cory Bloyd, released to the Public Domain.
  // http://paulbourke.net/geometry/polygonise/marchingsource.cpp

          (f[((k + 1) * ny + (j + 1)) * nx + (i + 0)] < threshold ? (1 << 6) : 0) |
          (f[((k + 1) * ny + (j + 1)) * nx + (i + 1)] < threshold ? (1 << 7) : 0);

        const unsigned N = mc_index_count[mccase];
        for (unsigned l = 0; l <N; l++) {
          const auto r = mc_triangles[16*mccase + l];

          vertices.push_back((2 * i + 2*((r >> 3) & 1) + ((r >> 0) & 1)) / (2.f * nx));
          vertices.push_back((2 * j + 2*((r >> 4) & 1) + ((r >> 1) & 1)) / (2.f * ny));
          vertices.push_back((2 * k + 2*((r >> 5) & 1) + ((r >> 2) & 1)) / (2.f * nz));
        }
#else
        unsigned mccase =
