diff --git a/uc_price_per_role.info b/uc_price_per_role.info
index 4c522e5..4213883 100644
--- a/uc_price_per_role.info
+++ b/uc_price_per_role.info
@@ -2,4 +2,4 @@ name = Price per role
 description = Allow Ubercart products to have role-specific pricing.
 dependencies[] = uc_product
 package = Ubercart - extra
-core = 6.x
+core = 7.x
diff --git a/uc_price_per_role.install b/uc_price_per_role.install
index 0c67440..6e1e05c 100644
--- a/uc_price_per_role.install
+++ b/uc_price_per_role.install
@@ -1,23 +1,15 @@
 <?php
 
 /**
- * Implementation of hook_install().
+ * @file
+ * Something
+ *
+ *
+ *
  */
-function uc_price_per_role_install() {
-  drupal_install_schema('uc_price_per_role');
-}
-
-/**
- * Implementation of hook_uninstall().
- */
-function uc_price_per_role_uninstall() {
-  drupal_uninstall_schema('uc_price_per_role');
-  variable_del('uc_price_per_role_enabled');
-  variable_del('uc_price_per_role_weights');
-}
 
 /**
- * Implementation of hook_schema().
+ * Implements hook_schema().
  */
 function uc_price_per_role_schema() {
   $schema = array();
@@ -98,51 +90,3 @@ function uc_price_per_role_schema() {
 
   return $schema;
 }
-
-/**
- * Add attribute options price table, update price precision. 
- */
-function uc_price_per_role_update_6000() {
-  $ret = array();
-
-  db_change_field($ret, 'uc_price_per_role_prices', 'price', 'price', array('type' => 'numeric', 'precision' => 16, 'scale' => 5, 'not null' => TRUE, 'default' => 0.0));
-  
-  db_create_table($ret, 'uc_price_per_role_option_prices', array(
-    'description' => t('Ubercart price per role option prices'),
-    'fields' => array(
-      'opid' => array(
-        'type' => 'serial',
-        'unsigned' => TRUE,
-        'not null' => TRUE,
-      ),
-      'nid' => array(
-        'type' => 'int',
-        'unsigned' => TRUE,
-        'not null' => TRUE,
-        'default' => 0,
-      ),
-      'oid' => array(
-        'type' => 'int',
-        'unsigned' => TRUE,
-        'not null' => TRUE,
-        'default' => 0,
-      ),
-      'rid' => array(
-        'type' => 'int',
-        'unsigned' => TRUE,
-        'not null' => TRUE,
-        'default' => 0,
-      ),
-      'price' => array(
-        'type' => 'numeric',
-        'precision' => 16,
-        'scale' => 5,
-        'not null' => TRUE,
-        'default' => 0.0,
-      ),
-    ),
-    'primary key' => array('opid'),
-  ));
-
-  return $ret;
-}
\ No newline at end of file
diff --git a/uc_price_per_role.migrate.inc b/uc_price_per_role.migrate.inc
index 485df00..1c45655 100644
--- a/uc_price_per_role.migrate.inc
+++ b/uc_price_per_role.migrate.inc
@@ -6,18 +6,18 @@
  */
 
 /**
- * Implement hook_migrate_fields_node().
+ * Implements hook_migrate_fields_node().
  */
 function uc_price_per_role_migrate_fields_node($type) {
   $enabled = variable_get('uc_price_per_role_enabled', array());
   $fields = array();
-  
+
     foreach (user_roles() as $rid => $role) {
       if ($enabled[$rid]) {
         $fields['role_prices_' . $rid] = t('Purchase price for %role users.', array('%role' => $role));
       }
     }
-    
+
   return $fields;
 }
 
@@ -25,15 +25,16 @@ function uc_price_per_role_migrate_prepare_node(&$node, $tblinfo, $row) {
   foreach ($tblinfo->fields as $destfield => $values) {
     if ($values['srcfield'] && isset($row->$values['srcfield'])) {
       $newvalue = $row->$values['srcfield'];
-    } else {
+    }
+    else {
       $newvalue = $values['default_value'];
     }
-    
-    if (drupal_substr($destfield, 0, strlen('role_prices_')) == 'role_prices_') {
-      $rid = drupal_substr($destfield, strlen('role_prices_'));
+
+    if (drupal_substr($destfield, 0, drupal_strlen('role_prices_')) == 'role_prices_') {
+      $rid = drupal_substr($destfield, drupal_strlen('role_prices_'));
       $node->role_prices[$rid] = $newvalue;
     }
   }
 
   return $errors;
-}
\ No newline at end of file
+}
diff --git a/uc_price_per_role.module b/uc_price_per_role.module
index 8b1288e..eb35a86 100644
--- a/uc_price_per_role.module
+++ b/uc_price_per_role.module
@@ -1,14 +1,19 @@
 <?php
 
 /**
- * Implementation of hook_perm().
+ * Implements hook_permission().
  */
-function uc_price_per_role_perm() {
-  return array('access price selection block');
+function uc_price_per_role_permission() {
+  return array(
+    'administer price per role' => array(
+      'title' => t('access price selection block'),
+      'description' => t('Allows a user to configure...'),
+    ),
+  );
 }
 
 /**
- * Implementation of hook_menu().
+ * Implements hook_menu().
  */
 function uc_price_per_role_menu() {
   $items = array();
@@ -21,6 +26,7 @@ function uc_price_per_role_menu() {
     'page arguments' => array('uc_price_per_role_settings_form'),
     'type' => MENU_NORMAL_ITEM,
   );
+
   if (module_exists('uc_attribute')) {
     $items['node/%node/edit/option_prices'] = array(
       'title' => 'Option prices',
@@ -35,9 +41,15 @@ function uc_price_per_role_menu() {
 
   return $items;
 }
+function uc_price_per_role_admin_paths() {
+  $paths = array(
+    'node/*/edit/option_prices' => TRUE
+  );
 
+  return $paths;
+}
 /**
- * Implementation of hook_menu_alter().
+ * Implements hook_menu_alter().
  */
 function uc_price_per_role_menu_alter(&$items) {
   // Make room in local menu for the 'Option prices' tab.
@@ -49,28 +61,23 @@ function uc_price_per_role_menu_alter(&$items) {
   }
 }
 
+
 /**
- * Implementation of hook_theme().
+ * Implements hook_theme().
  */
 function uc_price_per_role_theme() {
   return array(
-    'uc_price_per_role_settings_form' => array(
+    'uc_price_per_role_settings_form' =>  array(
       'file' => 'uc_price_per_role.module',
-      'arguments' => array(
-        'form' => NULL,
-      ),
+      'render element' => 'form',
     ),
     'uc_price_per_role_product_prices' => array(
       'file' => 'uc_price_per_role.module',
-      'arguments' => array(
-        'prices' => NULL,
-      ),
+      'render element' => 'prices'
     ),
     'uc_price_per_role_option_prices_form' => array(
       'file' => 'uc_price_per_role.module',
-      'arguments' => array(
-        'form' => NULL,
-      ),
+      'render element' => 'form'
     ),
   );
 }
@@ -81,20 +88,20 @@ function uc_price_per_role_settings_form() {
   $roles = user_roles();
 
   foreach (array_keys($roles) as $rid) {
+    if (!isset($enabled[$rid])) {
+      $enabled[$rid] = 0;
+    }
     if (!isset($weights[$rid])) {
       $weights[$rid] = 0;
     }
   }
   asort($weights);
 
-  $form['help'] = array(
-    '#value' => t('Enable roles that require separate pricing. For users with more than one matching role, the lightest weight role that has a price available will be used.'),
-  );
-
   $form['fields']['#tree'] = TRUE;
   foreach ($weights as $rid => $weight) {
     $form['fields'][$rid]['role'] = array(
-      '#value' => $roles[$rid],
+      '#type' => 'markup',
+      '#markup' => $roles[$rid],
     );
     $form['fields'][$rid]['enabled'] = array(
       '#type' => 'checkbox',
@@ -104,29 +111,37 @@ function uc_price_per_role_settings_form() {
       '#type' => 'weight',
       '#delta' => 5,
       '#default_value' => $weight,
+      '#attributes' => array('class' => array('role-weight'))
     );
   }
 
   $form['buttons']['submit'] = array('#type' => 'submit', '#value' => t('Save configuration') );
-
   return $form;
 }
 
-function theme_uc_price_per_role_settings_form($form) {
-  $output = drupal_render($form['help']);
+function theme_uc_price_per_role_settings_form($variables) {
+  $form = $variables['form'];
+
+  $output = '<p>' . t('Enable roles that require separate pricing. For users with more than one matching role, the lightest weight role that has a price available will be used.') . '</p>';
 
   $header = array(t('Enable'), t('Role'), t('Weight'));
   $rows = array();
-  foreach (element_children($form['fields']) as $field) {
-    $row = array();
-    $row[] = drupal_render($form['fields'][$field]['enabled']);
-    $row[] = drupal_render($form['fields'][$field]['role']);
-    $row[] = drupal_render($form['fields'][$field]['weight']);
-    $rows[] = $row;
+
+  if (!empty($form['fields'])) {
+    foreach (element_children($form['fields']) as $field) {
+      $row = array();
+      $row[] = drupal_render($form['fields'][$field]['enabled']);
+      $row[] = drupal_render($form['fields'][$field]['role']);
+      $row[] = drupal_render($form['fields'][$field]['weight']);
+      $rows[] = array('data' => $row, 'class' => array('draggable'));
+    }
   }
-  $output .= theme('table', $header, $rows);
 
-  $output .= drupal_render($form);
+  drupal_add_tabledrag('uc-price-per-roles', 'order', 'sibling', 'role-weight');
+  $output .= theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'uc-price-per-roles')));
+
+  $output .= drupal_render_children($form);
+
   return $output;
 }
 
@@ -144,10 +159,10 @@ function uc_price_per_role_settings_form_submit($form, &$form_state) {
 }
 
 /**
- * Implementation of hook_form_alter().
+ * Implements hook_form_alter().
  */
 function uc_price_per_role_form_alter(&$form, $form_state, $form_id) {
-  if ($form['#id'] == 'node-form' && isset($form['base']['prices'])) {
+  if ($form_id == 'product_node_form' or $form_id == 'new_node_form') {
     $enabled = variable_get('uc_price_per_role_enabled', array());
     $enabled_roles = array_filter($enabled);
 
@@ -164,32 +179,35 @@ function uc_price_per_role_form_alter(&$form, $form_state, $form_id) {
       '#tree' => TRUE,
     );
 
-    $role_prices = $form['#node']->role_prices;
+    $sign_flag = variable_get('uc_sign_after_amount', FALSE);
+    $currency_sign = variable_get('uc_currency_sign', '$');
+
+    if (isset($form['#node']->role_prices)) {
+      $role_prices = $form['#node']->role_prices;
+    }
+
     foreach (user_roles() as $rid => $role) {
-      if ($enabled[$rid]) {
+      if (array_key_exists($rid, $enabled) && $enabled[$rid]) {
         $form['base']['role_prices'][$rid] = array(
           '#type' => 'textfield',
           '#title' => t('%role price', array('%role' => $role)),
+          '#required' => FALSE,
           '#default_value' => isset($role_prices[$rid]) ? uc_store_format_price_field_value($role_prices[$rid]) : '',
           '#description' => t('Purchase price for %role users.', array('%role' => $role)),
-          '#size' => 20,
-          '#maxlength' => 35,
-          '#field_prefix' => variable_get('uc_sign_after_amount', FALSE) ? '' : variable_get('uc_currency_sign', '$'),
-          '#field_suffix' => variable_get('uc_sign_after_amount', FALSE) ? variable_get('uc_currency_sign', '$') : '',
+          '#size' => 15,
+          '#maxlength' => 15,
+          '#field_prefix' => $sign_flag ? '' : $currency_sign,
+          '#field_suffix' => $sign_flag ? $currency_sign : '',
         );
       }
     }
   }
-
-
-  else if ($form_id === 'uc_object_options_form') {
+  elseif ($form_id === 'uc_object_options_form') {
     $form['uc_price_per_role_note'] = array(
-      '#value' => '<p>'. t('Note: "Price" column is the default option price. To set option prices per role use the <a href="@link">options prices form</a>.', array('@link' => url('node/'. $form['id']['#value'] .'/edit/option_prices'))) .'</p>',
+      '#markup' => '<p>' . t('Note: "Price" column is the default option price. To set option prices per role use the <a href="@link">options prices form</a>.', array('@link' => url('node/' . $form['id']['#value'] . '/edit/option_prices'))) . '</p>',
     );
   }
-
-
-  else if (strpos($form_id, 'add_to_cart_form')) {
+  elseif (strpos($form_id, 'add_to_cart_form')) {
     // Modify product form attribute option prices.
     $format = variable_get('uc_attribute_option_price_format', 'adjustment');
     if (module_exists('uc_attribute') && $format != 'none') {
@@ -203,46 +221,48 @@ function uc_price_per_role_form_alter(&$form, $form_state, $form_id) {
       $priced_attributes = uc_attribute_priced_attributes($nid);
       $role_prices = uc_price_per_role_load_option_prices($nid);
 
-      foreach (element_children($form['attributes']) as $aid) {
-        if (isset($form['attributes'][$aid]['#options'])) {
-          $attribute = uc_attribute_load($aid, $nid, 'product');
-
-          $context = array(
-            'revision' => 'altered',
-            'type' => 'product',
-            'subject' => array(
-              'node' => $product,
-            ),
-            'field' => 'price',
-          );
-          $product_price = uc_price($product->sell_price, $context);
-
-          foreach (array_keys($form['attributes'][$aid]['#options']) as $oid) {
-            $price = uc_price_per_role_find_price($role_prices[$oid]);
-
-            if ($price !== FALSE) {
-              $context = array(
-                'revision' => 'formatted',
-                'type' => 'attribute_option',
-                'subject' => array(
-                  'attribute' => $attribute,
-                  'option' => $attribute->options[$oid],
-                ),
-                'field' => 'price',
-              );
-
-              switch ($format) {
-                case 'total':
-                  $display_price = in_array($aid, $priced_attributes) ? ', '. uc_price(($product_price + $price) * $qty, $context) : '';
-                  if (count($priced_attributes) == 1) {
+      if (!empty($form['attributes'])) {
+        foreach (element_children($form['attributes']) as $aid) {
+          if (isset($form['attributes'][$aid]['#options'])) {
+            $attribute = uc_attribute_load($aid, $nid, 'product');
+
+            $context = array(
+              'revision' => 'altered',
+              'type' => 'product',
+              'subject' => array(
+                'node' => $product,
+              ),
+              'field' => 'price',
+            );
+            $product_price = $product->sell_price;
+
+            foreach (array_keys($form['attributes'][$aid]['#options']) as $oid) {
+              $price = uc_price_per_role_find_price($role_prices[$oid]);
+
+              if ($price !== FALSE) {
+                $context = array(
+                  'revision' => 'formatted',
+                  'type' => 'attribute_option',
+                  'subject' => array(
+                    'attribute' => $attribute,
+                    'option' => $attribute->options[$oid],
+                  ),
+                  'field' => 'price',
+                );
+
+                switch ($format) {
+                  case 'total':
+                    $display_price = in_array($aid, $priced_attributes) ? ', ' . (($product_price + $price) * $qty) : '';
+                    if (count($priced_attributes) == 1) {
+                      break;
+                    }
+                  case 'adjustment':
+                    $display_price = ($price != 0 ? ', ' . ($price > 0 ? '+' : '') . $price * $qty : '');
                     break;
-                  }
-                case 'adjustment':
-                  $display_price = ($price != 0 ? ', '. ($price > 0 ? '+' : '') . uc_price($price * $qty, $context) : '');
-                  break;
-              }
+                }
 
-              $form['attributes'][$aid]['#options'][$oid] = $attribute->options[$oid]->name . $display_price;
+                $form['attributes'][$aid]['#options'][$oid] = $attribute->options[$oid]->name . $display_price;
+              }
             }
           }
         }
@@ -251,111 +271,163 @@ function uc_price_per_role_form_alter(&$form, $form_state, $form_id) {
   }
 }
 
-function theme_uc_price_per_role_product_prices($prices) {
-  $output = '<table><tr>';
-  foreach (element_children($prices) as $rid) {
-    $output .= '<td>'. drupal_render($prices[$rid]) .'</td>';
+function theme_uc_price_per_role_product_prices($variables) {
+  $prices = $variables['prices'];
+
+  $output = '';
+  if (!empty($prices)) {
+    $output .= '<table><tr>';
+    foreach (element_children($prices) as $rid) {
+      $output .= '<td>' . drupal_render($prices[$rid]) . '</td>';
+    }
+    $output .= "</table>\n";
   }
-  $output .= "</table>\n";
   return $output;
 }
 
 /**
- * Implementation of hook_nodeapi().
+ * Implements hook_node_validate().
  */
-function uc_price_per_role_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
-  global $user;
+function uc_price_per_role_node_validate($node, $form, &$form_state) {
+  if (isset($node->role_prices)) {
+    foreach ($node->role_prices as $rid => $price) {
+      if (!empty($price) && !is_numeric($price)) {
+        form_set_error('role_prices][' . $rid, t('Price must be a number.'));
+      }
+    }
+  }
+}
+/**
+ * Implements hook_node_load().
+ */
+function uc_price_per_role_node_load($nodes, $types) {
+  $node_array = array();
 
-  if (in_array($node->type, uc_product_types())) {
-    switch ($op) {
-      case 'validate':
-        if (isset($node->role_prices)) {
-          foreach ($node->role_prices as $rid => $price) {
-            if (!empty($price) && !is_numeric($price)) {
-              form_set_error('role_prices]['. $rid, t('Price must be a number.'));
-            }
-          }
-        }
-        break;
-
-      case 'insert':
-      case 'update':
-        db_query("DELETE FROM {uc_price_per_role_prices} WHERE nid = %d", $node->nid);
-        if (is_array($node->role_prices)) {
-          foreach ($node->role_prices as $rid => $price) {
-            if (is_numeric($price)) {
-              db_query("INSERT INTO {uc_price_per_role_prices} (vid, nid, rid, price) VALUES (%d, %d, %d, %f)", $node->vid, $node->nid, $rid, $price);
-            }
-          }
-        }
-        break;
+  foreach ($nodes as $node) {
+    if (in_array($node->type, uc_product_types())) {
+      $node->role_prices = array();
+      $node_array[$node->vid] = $node;
+    }
+  }
 
-      case 'load':
-        $result = db_query("SELECT rid, price FROM {uc_price_per_role_prices} WHERE vid = %d", $node->vid);
-        $prices = array();
-        while ($row = db_fetch_object($result)) {
-          $prices[$row->rid] = $row->price;
-        }
+  if (!empty($node_array)) {
+    $result = db_query("SELECT nid, rid, price FROM {uc_price_per_role_prices} WHERE vid IN(:vids)",
+      array(':vids' => array_keys($node_array)));
+    foreach ($result as $key => $row) {
+      $node_array[$row->nid]->role_prices[$row->rid] = $row->price;
+    }
+    foreach ($node_array as $node) {
+      $original_price = $node->sell_price;
+      $price = uc_price_per_role_find_price($node->role_prices);
+      if ($price !== FALSE) {
+        $node->sell_price = $price;
+      }
+      $node->roleindependent_sell_price = $original_price;
+    }
+  }
+}
 
-        $original_price = $node->sell_price;
-        $price = uc_price_per_role_find_price($prices);
-        if ($price !== FALSE) {
-          $node->sell_price = $price;
-        }
+/**
+ * Implements hook_node_insert().
+ */
+function uc_price_per_role_node_insert($node) {
+  db_query("DELETE FROM {uc_price_per_role_prices} WHERE nid = :nid", array(':nid' => $node->nid));
+  if (is_array($node->role_prices)) {
+    foreach ($node->role_prices as $rid => $price) {
+      if (is_numeric($price)) {
+        db_query("INSERT INTO {uc_price_per_role_prices} (vid, nid, rid, price) VALUES (:vid, :nid, :rid, :price)", array(':vid' => $node->vid, ':nid' => $node->nid, ':rid' => $rid, ':price' => $price));
+      }
+    }
+  }
+}
+
+/**
+ * Implements hook_node_prepare().
+ */
+
+function uc_price_per_role_node_prepare($node) {
+  if (isset($node->roleindependent_sell_price)) {
+     // Reload original price for editing.
+    $node->sell_price = $node->roleindependent_sell_price;
+  }
+}
+
+/**
+ * Implements hook_node_update().
+ */
+function uc_price_per_role_node_update($node) {
+  db_query("DELETE FROM {uc_price_per_role_prices} WHERE nid = :nid", array(':nid' => $node->nid));
+  if (is_array($node->role_prices)) {
+    foreach ($node->role_prices as $rid => $price) {
+      if (is_numeric($price)) {
+        db_query("INSERT INTO {uc_price_per_role_prices} (vid, nid, rid, price) VALUES (:vid, :nid, :rid, :price)", array(':vid' => $node->vid, ':nid' => $node->nid, ':rid' => $rid, ':price' => $price));
+      }
+    }
+  }
+}
 
-        return array('role_prices' => $prices, 'roleindependent_sell_price' => $original_price);
+/**
+ * Find the price for the current user from the supplied price array.
+ *
+ * Will return FALSE if no price was found.
+ */
+function uc_price_per_role_find_price($prices) {
+  global $user;
 
-      case 'prepare':
-        // Reload original price for editing.
-        $node->sell_price = $node->roleindependent_sell_price;
-        break;
+  $enabled = variable_get('uc_price_per_role_enabled', array());
+  $weights = variable_get('uc_price_per_role_weights', array());
+  asort($weights);
 
-      case 'delete':
-        db_query("DELETE FROM {uc_price_per_role_prices} WHERE nid = %d", $node->nid);
-        break;
+  // Check for a chosen role in the price selection block first.
+  if (user_access('access price selection block') && isset($_SESSION['price_role']) && isset($prices[$_SESSION['price_role']])) {
+    return $prices[$_SESSION['price_role']];
+  }
 
-      case 'delete revision':
-        db_query("DELETE FROM {uc_price_per_role_prices} WHERE vid = %d", $node->vid);
-        break;
+  // Otherwise, look for a matching role.
+  foreach ($weights as $rid => $weight) {
+    if (isset($user->roles[$rid]) && $enabled[$rid] && isset($prices[$rid])) {
+      return $prices[$rid];
     }
   }
+
+  return FALSE;
 }
 
 /**
- * Implementation of hook_cart_item().
+ * Implements hook_uc_cart_item().
  */
-function uc_price_per_role_cart_item($op, &$item) {
+function uc_price_per_role_uc_cart_item($op, $item) {
   if (module_exists('uc_attribute')) {
     switch ($op) {
       case 'load':
         $role_prices = uc_price_per_role_load_option_prices($item->nid);
         foreach (_uc_cart_product_get_options($item) as $option) {
           $oid = $option['oid'];
-          $price = uc_price_per_role_find_price($role_prices[$oid]);
-          if ($price !== FALSE) {
-            $item->price += $price - $option['price'];
+          if (isset($role_prices[$oid])) {
+            $price = uc_price_per_role_find_price($role_prices[$oid]);
+            if ($price !== FALSE) {
+              $item->price += $price - $option['price'];
+            }
           }
         }
     }
   }
 }
 
-/**
- * Form definition to edit a option prices.
- */
-function uc_price_per_role_option_prices_form($form_state, $product) {
-  drupal_set_title(check_plain($product->title));
+function uc_price_per_role_option_prices_form($form, &$form_state, $object) {
+
+  drupal_set_title(check_plain($object->title));
 
-  $role_prices = uc_price_per_role_load_option_prices($product->nid);
+  $role_prices = uc_price_per_role_load_option_prices($object->nid);
   $enabled = array_keys(array_filter(variable_get('uc_price_per_role_enabled', array())));
-  
+
   $form['help'] = array(
-    '#value' => t('Leave any box blank to use the default price for the option.'),
+    '#markup' => t('Leave any box blank to use the default price for the option.'),
   );
 
-  foreach (uc_product_get_attributes($product->nid) as $aid => $attribute) {
+  foreach (uc_product_get_attributes($object->nid) as $aid => $attribute) {
     $form['attributes'][$aid]['name'] = array(
-      '#value' => $attribute->name,
+      '#markup' => $attribute->name,
     );
     $form['attributes'][$aid]['aid'] = array(
       '#type' => 'hidden',
@@ -381,9 +453,10 @@ function uc_price_per_role_option_prices_form($form_state, $product) {
     if ($base_attr->options) {
       $result = db_query("
         SELECT ao.aid, ao.oid, ao.name, ao.price AS default_price, ao.ordering AS default_ordering, po.price, po.ordering, po.ordering IS NULL AS null_order FROM {uc_attribute_options} AS ao
-        LEFT JOIN {uc_product_options} AS po ON ao.oid = po.oid AND po.nid = %d
-        WHERE aid = %d ORDER BY null_order, po.ordering, default_ordering, ao.name", $product->nid, $attribute->aid);
-      while ($option = db_fetch_object($result)) {
+        LEFT JOIN {uc_product_options} AS po ON ao.oid = po.oid AND po.nid = :nid
+        WHERE aid = :aid ORDER BY null_order, po.ordering, default_ordering, ao.name", array(':nid' => $object->nid, ':aid' => $attribute->aid));
+
+      foreach ($result as $option) {
         $oid = $option->oid;
 
         $context['revision'] = 'themed';
@@ -391,17 +464,17 @@ function uc_price_per_role_option_prices_form($form_state, $product) {
         $price = is_null($option->price) ? $option->default_price : $option->price;
 
         $form['attributes'][$aid]['options'][$oid]['name'] = array(
-          '#value' => $option->name,
+          '#markup' => $option->name,
         );
 
         $form['attributes'][$aid]['options'][$oid]['price'] = array(
-          '#value' => uc_price($price, $context),
+          '#markup' => $price,
         );
         // Include unformatted original price to simplify form alteration.
         $context['revision'] = 'original';
         $form['attributes'][$aid]['options'][$oid]['price_value'] = array(
           '#type' => 'value',
-          '#value' => uc_price($price, $context),
+          '#value' => $price,
         );
 
         foreach ($enabled as $rid) {
@@ -432,16 +505,20 @@ function uc_price_per_role_option_prices_form($form_state, $product) {
 
   $form['nid'] = array(
     '#type' => 'value',
-    '#value' => $product->nid,
+    '#value' => $object->nid,
   );
-
   return $form;
 }
 
 /**
- * Theme form to edit option prices.
+ * Display the option form.
+ *
+ * @ingroup themeable
+ * @see uc_object_options_form()
  */
-function theme_uc_price_per_role_option_prices_form($form) {
+function theme_uc_price_per_role_option_prices_form($variables) {
+  $form = $variables['form'];
+
   $roles = user_roles();
   $enabled = array_keys(array_filter(variable_get('uc_price_per_role_enabled', array())));
 
@@ -450,29 +527,30 @@ function theme_uc_price_per_role_option_prices_form($form) {
     $header[] = t('%role price', array('%role' => $roles[$rid]));
   }
 
-  foreach (element_children($form['attributes']) as $key) {
-    $row = array();
-    $row[] = array('data' => drupal_render($form['attributes'][$key]['aid']) . drupal_render($form['attributes'][$key]['name']), 'class' => 'attribute');
+  if (!empty($form['attributes'])) {
+    foreach (element_children($form['attributes']) as $key) {
+      $row = array();
+      $row[] = array('data' => drupal_render($form['attributes'][$key]['aid']) . drupal_render($form['attributes'][$key]['name']), 'class' => 'attribute');
+      if (!empty($form['attributes'][$key]['options']) && $children = element_children($form['attributes'][$key]['options'])) {
+        foreach ($children as $oid) {
+          $row[] = drupal_render($form['attributes'][$key]['options'][$oid]['name']);
+          $row[] = drupal_render($form['attributes'][$key]['options'][$oid]['price']);
+          foreach ($enabled as $rid) {
+            $row[] = drupal_render($form['attributes'][$key]['options'][$oid]['role_prices'][$rid]);
+          }
 
-    if (element_children($form['attributes'][$key]['options'])) {
-      foreach (element_children($form['attributes'][$key]['options']) as $oid) {
-        $row[] = drupal_render($form['attributes'][$key]['options'][$oid]['name']);
-        $row[] = drupal_render($form['attributes'][$key]['options'][$oid]['price']);
-        foreach ($enabled as $rid) {
-          $row[] = drupal_render($form['attributes'][$key]['options'][$oid]['role_prices'][$rid]);
+          $rows[] = $row;
+          $row = array('');
         }
-
+        unset($form['attributes'][$key]['default']);
+      }
+      else {
+        $row[] = array('data' => drupal_render($form['attributes'][$key]['default']), 'colspan' => count($enabled) + 3);
         $rows[] = $row;
-        $row = array('');
       }
-      unset($form['attributes'][$key]['default']);
-    }
-    else {
-      $row[] = array('data' => drupal_render($form['attributes'][$key]['default']), 'colspan' => count($enabled) + 3);
-      $rows[] = $row;
-    }
 
-    $rows[] = array(array('data' => '<hr />', 'colspan' => count($enabled) + 3));
+      $rows[] = array(array('data' => '<hr />', 'colspan' => count($enabled) + 3));
+    }
   }
 
   if (count($rows) == 0) {
@@ -482,20 +560,31 @@ function theme_uc_price_per_role_option_prices_form($form) {
   }
 
   $output = drupal_render($form['help'])
-          . theme('table', $header, $rows, array('class' => 'product_attributes'))
-          . drupal_render($form);
+          . theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'product_attributes')))
+          . drupal_render_children($form);
 
   return $output;
 }
 
 /**
+ * Load per-role option price data for the supplied node ID.
+ */
+function uc_price_per_role_load_option_prices($nid) {
+  $prices = array();
+  $result = db_query("SELECT oid, rid, price FROM {uc_price_per_role_option_prices} WHERE nid = :nid", array(':nid' => $nid));
+  foreach ($result as $row) {
+    $prices[$row->oid][$row->rid] = $row->price;
+  }
+  return $prices;
+}
+/**
  * Form submit handler to save option prices.
  */
 function uc_price_per_role_option_prices_form_submit($form_id, &$form_state) {
   foreach ($form_state['values']['attributes'] as $attribute) {
     if (is_array($attribute['options'])) {
       foreach ($attribute['options'] as $oid => $option) {
-        db_query("DELETE FROM {uc_price_per_role_option_prices} WHERE nid = %d AND oid = %d", $form_state['values']['nid'], $oid);
+        db_query("DELETE FROM {uc_price_per_role_option_prices} WHERE nid = :nid AND oid = :oid", array(':nid' => $form_state['values']['nid'], ':oid' => $oid));
 
         foreach ($option['role_prices'] as $rid => $price) {
           if (is_numeric($price)) {
@@ -516,53 +605,15 @@ function uc_price_per_role_option_prices_form_submit($form_id, &$form_state) {
 }
 
 /**
- * Load per-role option price data for the supplied node ID.
- */
-function uc_price_per_role_load_option_prices($nid) {
-  $prices = array();
-  $result = db_query("SELECT oid, rid, price FROM {uc_price_per_role_option_prices} WHERE nid = %d", $nid);
-  while ($row = db_fetch_object($result)) {
-    $prices[$row->oid][$row->rid] = $row->price;
-  }
-  return $prices;
-}
-
-/**
- * Find the price for the current user from the supplied price array.
- * 
- * Will return FALSE if no price was found.
+ * Implements hook_block_info().
  */
-function uc_price_per_role_find_price($prices) {
-  global $user;
-
-  $enabled = variable_get('uc_price_per_role_enabled', array());
-  $weights = variable_get('uc_price_per_role_weights', array());
-  asort($weights);
-
-  // Check for a chosen role in the price selection block first.
-  if (user_access('access price selection block') && $_SESSION['price_role'] && isset($prices[$_SESSION['price_role']])) {
-    return $prices[$_SESSION['price_role']];
-  }
-
-  // Otherwise, look for a matching role.
-  foreach ($weights as $rid => $weight) {
-    if (isset($user->roles[$rid]) && $enabled[$rid] && isset($prices[$rid])) {
-      return $prices[$rid];
-    }
-  }
-
-  return FALSE;
-}
+function uc_price_per_role_block_info() {
 
-/**
- * Implementation of hook_block().
- */
-function uc_price_per_role_block($op = 'list', $delta = 0, $edit = array()) {
-  if ($op == 'list') {
     $blocks[0]['info'] = t('Pricing selection');
     return $blocks;
-  }
-  else if ($op == 'view' && $delta == 0 && user_access('access price selection block')) {
+}
+function uc_price_per_role_block_view($delta = 0) {
+  if ($delta == 0 && user_access('access price selection block')) {
     $block = array(
       'subject' => t('Pricing selection'),
       'content' => drupal_get_form('uc_price_per_role_switch_form'),
@@ -612,7 +663,7 @@ function uc_price_per_role_switch_form_submit($form_id, &$form_state) {
 }
 
 /**
- * Implementation of hook_migrate_api().
+ * Implements of hook_migrate_api().
  */
 function uc_price_per_role_migrate_api() {
   return array('api' => 1);
