   }
   afTimeCurve_t* host_curve = (afTimeCurve_t*) host_mem;
   // Check that the memory copied actually makes sense.
-  if (host_curve->pairs == src + sizeof(afTimeCurve_t) && host_curve->count == count) {
+  if (host_curve->pairs == (afTimeCurvePair_t*) ((char*)src + sizeof(afTimeCurve_t)) && host_curve->count == count) {
     // Copy worked!
     host_curve->pairs = (afTimeCurvePair_t*) host_curve + sizeof(afTimeCurve_t);
     (*dst) = host_curve;
     return result;
-  } else if (host_curve->pairs == src + sizeof(afTimeCurve_t) && host_curve->count < count ) {
+  } else if (host_curve->count < 0) {
+    // We seem to have copied the incorrect thing...
+    return cudaErrorUnknown;
+  } else if (host_curve->pairs == (afTimeCurvePair_t*) ((char*)src + sizeof(afTimeCurve_t)) && host_curve->count < count ) {
     // We over allocated on the host. Lets deallocate some memory.
-    afTimeCurve_t* new_host_curve = realloc(host_curve, sizeof(afTimeCurve_t) + host_curve->count * sizeof(afTimeCurvePair_t));
+    afTimeCurve_t* new_host_curve = (afTimeCurve_t*) realloc(host_curve, sizeof(afTimeCurve_t) + host_curve->count * sizeof(afTimeCurvePair_t));
     if (new_host_curve == NULL) {
       // Try and free host allocated memory.
       free(host_mem);
       return cudaErrorUnknown;
     }
-    host_curve->pairs = (afTimeCurvePair_t*) host_curve + sizeof(afTimeCurve_t);
+    host_curve->pairs = (afTimeCurvePair_t*) ((char*)host_curve + sizeof(afTimeCurve_t));
     (*dst) = new_host_curve;
     return result;
-  } else if (host_curve->pairs == src + sizeof(afTimeCurve_t) && host_curve->count > count) {
+  } else if (host_curve->pairs == (afTimeCurvePair_t*) ((char*)src + sizeof(afTimeCurve_t)) && host_curve->count > count) {
     // Extend the memory allocated a bit because we under allocated on the host.
-    afTimeCurve_t* new_host_curve = realloc(host_curve, sizeof(afTimeCurve_t) + host_curve->count * sizeof(afTimeCurvePair_t));
+    afTimeCurve_t* new_host_curve = (afTimeCurve_t*) realloc(host_curve, sizeof(afTimeCurve_t) + host_curve->count * sizeof(afTimeCurvePair_t));
     if (new_host_curve == NULL) {
       free(host_mem);
       return cudaErrorUnknown;
     }
     // We'll now copy down the additional pairs.
+    afTimeCurvePair_t* dev_start_ptr = (afTimeCurvePair_t*) ((char*)src + count * sizeof(afTimeCurvePair_t));
+    int length = new_host_curve->count - count;
+    // Use void* here
+    afTimeCurvePair_t* host_start_ptr = (afTimeCurvePair_t*) ((char*)new_host_curve + count * sizeof(afTimeCurvePair_t));
+    result = cudaMemcpy(host_start_ptr, dev_start_ptr, length * sizeof(afTimeCurvePair_t), cudaMemcpyDeviceToHost);
+    if (result != cudaSuccess) {
+      free(new_host_curve);
+      return result;
+    }
+    // Fix up the pointer.
+    new_host_curve->pairs = (afTimeCurvePair_t*) ((char*)new_host_curve + sizeof(afTimeCurve_t));
+    // Return the result.
+    (*dst) = new_host_curve;
+    return result;
   } else {
     // We copied memory that was unrelated. Lets reallocate what we have and try again.
-  }
-}
+    afTimeCurve_t* new_host_curve = (afTimeCurve_t*) realloc(host_curve, sizeof(afTimeCurve_t) + host_curve->count * sizeof(afTimeCurvePair_t));
+    if(new_host_curve == NULL) {
+      free(new_host_curve);
+      return cudaErrorUnknown;
+    }
+    afTimeCurvePair_t* host_start_ptr = (afTimeCurvePair_t*) ((char*)new_host_curve + sizeof(afTimeCurve_t));
+    result = cudaMemcpy(host_start_ptr, new_host_curve->pairs, new_host_curve->count * sizeof(afTimeCurvePair_t), cudaMemcpyDeviceToHost);
+    if (result != cudaSuccess) {
+      free(new_host_curve);
+      return cudaErrorUnknown;
+    }
+    (*dst) = new_host_curve;
+    return result;
+  }
+}

}

__host__
cudaError_t __af_TimeCurveCopyToDevice(afTimeCurve_t* src, afTimeCurve_t** dst) {
  void* dev_mem;
  cudaError_t result;
  // We attempt to put a whole curve in a contiguous block of memory.

      cudaFree(dev_mem);
      return result;
    }
    result      = cudaMemcpy((char*)dev_mem + sizeof(afTimeCurve_t), src->pairs, src->count * sizeof(afTimeCurvePair_t), cudaMemcpyHostToDevice);
    if (result != cudaSuccess) {
      // Try and free the allocated memory.
      cudaFree(dev_mem);
