Can someone help me tune the Image to Imagefield script to work properly with the Nodereference Image Helper module.

The original migration script can be found here: http://drupal.org/node/432860

Here is the the Image Attach part that should be modified:

// Loop over the image_attach records
if (module_exists('image_attach')) {
  $table_image_attach = $table_pfx. 'image_attach'; // add prefix
  $sql = "SELECT n.nid, n.vid, ia.iid FROM {$table_image_attach} ia INNER JOIN {$table_node} n ON ia.nid=n.nid";
  $result = db_query($sql);
  $num = 0;
  while ($row = db_fetch_object($result)) {
    $num++;
    // UPDATE the imagefield to point to the attached node, not the standalone node
    $sql = "UPDATE $table_content_type SET nid = $row->nid, vid = $row->vid WHERE nid = $row->iid";
    if (!db_query($sql)) {
      echo "update $table_content_type failed for $row->iid<br />\n";
    }
    $sql = "UPDATE $table_content_field SET nid = $row->nid, vid = $row->vid WHERE nid = $row->iid";
    if (db_query($sql)) {
      // Successful update. Now unpublish the standalone node that we just made.
      $sql = "UPDATE {$table_node} SET status = 0 WHERE nid = $row->iid";
      db_query($sql);
    }
    else {
      echo "update $table_content_field failed for $row->iid<br />\n";
    }
  }
  echo "- $num image_attach relationships were migrated.<br />\n";
}