diff -u -b userpoints_ubercart/uc_userpoints_discount.module /Users/mradcliffe/Desktop/userpoints_ubercart/uc_userpoints_discount.module
--- userpoints_ubercart/uc_userpoints_discount.module	2009-05-13 22:50:03.000000000 -0400
+++ /Users/mradcliffe/Desktop/userpoints_ubercart/uc_userpoints_discount.module	2009-04-28 05:50:36.000000000 -0400
@@ -1,11 +1,11 @@
 <?php
 
-//$Id: uc_userpoints_discount.module,v 1.4.2.1 2009/05/14 02:47:13 smokris Exp $
+//$Id: uc_userpoints_discount.module,v 1.4 2008/07/27 23:05:40 bmagistro Exp $
 
 define('USERPOINTS_UC_DISC',   'userpoints_ubercart_payment');
 define('USERPOINTS_DISCOUNT',     'userpoints_discount');
 define('UC_USERPOINTS_DISCOUNT_MODERATE',      'uc_userpoints_discount_moderate');
-define('UC_USERPOINTS_DISCOUNT_MAXP',      'uc_userpoints_discount_maxp');
+define('USERPOINTS_DISCOUNT_CATEGORY',		'userpoints_discount_category');
 
 function uc_userpoints_discount_help($path, $arg) {
   switch ($path) {
@@ -34,13 +34,6 @@
           '#size' => 5,
           '#maxlength' => 5,
         );
-        $form[$group][UC_USERPOINTS_DISCOUNT_MAXP] = array(
-          '#type' => 'textfield',
-          '#title' => t('Enter the maximum percentage of the order total that can be discounted using points. Please do not include a % sign'),
-          '#default_value' => variable_get(UC_USERPOINTS_DISCOUNT_MAXP, 100),
-          '#size' => 5,
-          '#maxlength' => 5,
-        );
         $form[$group][USERPOINTS_DISCOUNT] = array(
           '#type' => 'radios',
           '#title' => t('Enable !Points as a discount method', userpoints_translation()),
@@ -53,12 +46,22 @@
           '#default_value' => variable_get(UC_USERPOINTS_DISCOUNT_MODERATE, 0),
 		  '#options' => array(t('No'), t('Yes')),
         );
+
+				$form[$group][USERPOINTS_DISCOUNT_CATEGORY] = array(
+					'#type' => 'select',
+					'#title' => t('Discount Category'),
+					'#default_value' => variable_get(USERPOINTS_DISCOUNT_CATEGORY, NULL),
+					'#options' =>  userpoints_get_categories(),
+					'#description' => t('Category to assign awarded !points.', userpoints_translation()),
+				);
+
       }
       return $form;
       break;
   }
 }
 
+
 /**
  * Implementation of hook_checkout_pane
  */
@@ -70,7 +73,7 @@
     
   if ($discMethod != 0 && ($curUserPoints > 0 || request_uri() != '/cart/checkout')) {
     $panes[] = array(
-      'id' => 'uc_up_discounts',
+      'id' => 'up_uc_discounts',
       'title' => t('!Points Discounts', userpoints_translation()),
       'desc' => t('Show !Points discounts for the order', userpoints_translation()),
       'callback' => 'uc_checkout_pane_uc_userpoints_discount',
@@ -89,30 +92,25 @@
   switch ($op) {
     case 'view':
 			$curUserPoints = intval(userpoints_get_current_points($uid = $curUserId, $tid = NULL));
-			$maxdisc = ($curUserPoints / intval(variable_get(USERPOINTS_UC_DISC, 1)));
-			$maxpt = variable_get(UC_USERPOINTS_DISCOUNT_MAXP, 100);
-			$description = t('Enter the amount to discount this order with !Points by.  You cannot discount more than ' . $maxpt . '% of this order.  You have a total of ' . uc_currency_format($maxdisc) . ' available via !Points. This field will be ignored if paying with !Points.', userpoints_translation());
+			$maxdisc = round($curUserPoints / intval(variable_get(USERPOINTS_UC_DISC, 1)));
+			$description = t('Enter the amount to discount this order with !Points by (Total available via !Points: ' . uc_currency_format($maxdisc) . ') This field will be ignored if paying with !Points.', userpoints_translation());
 			$contents['ptamt'] = array(
 				'#type' => 'textfield',
 				'#title' => t('Discount Amount'),
 				'#description' => t(''),
-				'#default_value' => $arg1->ptamt,
+        '#default_value' => $arg1->ptdiscount['amt'],
 			);
+
 			return array('description' => $description, 'contents' => $contents);
 			break;	
     case 'process':
-			$ptamt = $arg2['ptamt'];
-			if (!empty($ptamt) || $ptamt != '') {
-				$arg1->ptamt = $ptamt;
-			}
-			else {
-				$arg1->ptamt = 0;
-			}
+			$arg1->ptdiscount['amt'] = $arg2['ptamt'];
 			return TRUE;
 			break;
   }
 }
 
+
 /**
  * Implementation of hook_order().
  * This is where all the important stuff happens like taking the points...
@@ -134,47 +132,19 @@
 			}
 			break;
     case 'save':
-			db_query("DELETE FROM {uc_updiscounts} WHERE oid=%d", $arg1->order_id);
-			db_query("DELETE FROM {uc_order_line_items} WHERE order_id=%d AND type='ptdiscount'", $arg1->order_id);
-			uc_line_items_calculate($arg1->order_id);	
+			if (variable_get(USERPOINTS_DISCOUNT,1) != 0 && strtolower($order->payment_method) != 'points') {
 			global $user;
 			$curUserId = $user->uid;
-			$pagepath = str_replace('destination=','',urldecode(drupal_get_destination()));
-			$multiplier 	= (variable_get(USERPOINTS_UC_DISC, 0));
-			$maxdisc = variable_get(UC_USERPOINTS_DISCOUNT_MAXP, 100)/100;
-
-			#money value of order
-			$orderamt = $arg1->order_total + $arg1->quote['rate'];
-			
-			#discount value
-			$ptamt = $arg1->ptamt;
-			#discount value in points
-			$points = -(($ptamt) * $multiplier);
-			$ptdisc = (($ptamt) * $multiplier);
-			
-			#max discount on order total
-			$ptmaxd = $maxdisc * $orderamt;			
+				$ptamt = $arg1->ptdiscount['amt'];
+				$multiplier 	= intval(variable_get(USERPOINTS_UC_DISC, 0));
+				$points = -intval(round($ptamt) * $multiplier);
 			
-			if (variable_get(USERPOINTS_DISCOUNT,1) != 0 && strtolower($order->payment_method) != 'points') {
-				if ((!empty($ptamt) || $ptamt != '') && $ptamt < $orderamt && $ptamt <= $ptmaxd) {
 					db_query("DELETE FROM {uc_order_line_items} WHERE order_id=%d AND type='ptdiscount'", $arg1->order_id);
+
+				if ((!empty($ptamt) || $ptamt != '') && $arg1->payment_method != 'points') {
 					db_query('INSERT INTO {uc_updiscounts} (uid, oid, ptamt, points) VALUES (%d, %d, \'%f\', %d)', $curUserId, $arg1->order_id, $ptamt, $points);
 					uc_order_line_item_add($arg1->order_id, 'ptdiscount', t('Discount order using !Points',userpoints_translation()), -$ptamt, 1);
 				}
-				elseif ($ptdisc <= $orderamt && (!empty($ptamt) || $ptamt != '')) {
-					#this should be to check it greater than order total
-					drupal_set_message('You cannot discount an order for more than the order total.', 'error');
-					drupal_goto($pagepath);
-				}
-				elseif ($ptdisc > $ptmaxd && (!empty($ptamt) || $ptamt != '')) {
-					#this should be to check if discount greater than max
-					drupal_set_message(t('The maximum you can discount this order by using points is $' . $ptmaxd . '.  You need to decrease the size of the discount.', userpoints_translation()), 'error');
-					drupal_goto($pagepath);
-				}
-			}
-			elseif (strtolower($order->payment_method) == 'points' && (!empty($ptamt) || $ptamt != '')) {
-					drupal_set_message(t('You cannot use !Points to pay for a product and use !Points as a discount method.', userpoints_translation()), 'error');
-					drupal_goto($pagepath);
 			}
 			break;
 		case 'submit':
@@ -183,16 +153,16 @@
 				if (variable_get(USERPOINTS_DISCOUNT,1) != 0) {
 					global $user;
 					$curUserId = $user->uid;
-					$ptamt = $arg1->ptamt;
+					$ptamt = $arg1->ptdiscount['amt'];
 					$multiplier 	= intval(variable_get(USERPOINTS_UC_DISC, 0));
 				
 					// Payment completed
 					if ($curUserId != 0) {
 					// User id from the transaction
-						$points = intval(($ptamt) * $multiplier);
+						$points = intval(round($ptamt) * $multiplier);
 						$points = -$points;
 						$params = array (
-						'tid' => 0,
+							'tid' => variable_get(USERPOINTS_DISCOUNT_CATEGORY, 0),
 						'uid' => $curUserId,
 						'points' => $points,
 						'operation' => 'delete',
@@ -202,18 +172,17 @@
 						'moderate' => variable_get(UC_USERPOINTS_DISCOUNT_MODERATE, 0),
 						);
 						userpoints_userpointsapi($params);
-					db_query('INSERT INTO {uc_updiscounts} (uid, oid, ptamt, points) VALUES (%d, %d, \'%f\', %d)', $curUserId, $arg1->order_id, $ptamt, $points);
-					// db_query("insert into {uc_uplog} (uid, oid, points, referrer, discount) values(%d, %d, %d, 0, 1)",$curUserId, $order->order_id, $points);
+						//mnencia: WTF is this? // db_query("insert into {uc_uplog} (uid, oid, points, referrer, discount) values(%d, %d, %d, 0, 1)",$curUserId, $order->order_id, $points);
 					}
 
 				}
 			}
 			break;
 		case 'load':
-			$arg1->ptamt = '';
+			$arg1->ptdiscount['amt'] = '';
 			$result = db_query("SELECT * FROM {uc_updiscounts} WHERE oid = %d", $arg1->order_id);
 			if ($data = db_fetch_object($result)) {
-				$arg1->ptamt = $data->ptamt;
+				$arg1->ptdiscount['amt'] = $data->ptamt;
 			}
 			break;
 		case 'delete':	
@@ -221,15 +190,14 @@
 				global $user;
 				$curUserId	= $user->uid;
 				$oid			= $order->order_id;
-				$result 		= db_query("select updlog_id, points from {uc_updiscounts} where oid = %d and uid = %d and referrer = 0 and discount =1", $oid, $curUserId);
+				$result 		= db_query("select updlog_id, points from {uc_updiscounts} where oid = %d and uid = %d", $oid, $curUserId);
 				
-				if ($result != FALSE) {
-					$pointinfo = db_fetch_object($result);
+				if ($result != FALSE && $pointinfo = db_fetch_object($result) != FALSE) {
 					$points = intval(($pointinfo->points) * (-1));
 					$description = 'User Discount (Ubercart Order ' . $oid . ') that has been deleted.';
 					
 					$params = array (
-						'tid' => 0,
+						'tid' => variable_get(USERPOINTS_DISCOUNT_CATEGORY, 0),
 						'uid' => $curUserId,
 						'points' => $points,
 						'operation' => 'delete',
@@ -240,10 +208,13 @@
 					);
 					
 					userpoints_userpointsapi($params);
-					db_query("delete from {uc_updiscounts} where uplog_id = %d", $pointinfo->uplog_id);
+					db_query("delete from {uc_updiscounts} where updlog_id = %d", $pointinfo->uplog_id);
 					db_query("DELETE FROM {uc_order_line_items} WHERE order_id=%d AND type='ptdiscount'", $arg1->order_id);
 				}
 			}
 			break;
+		case 'can_delete';
+			return FALSE;
+			break;
   }
 }
--- userpoints_ubercart/uc_userpoints_payment.module	2009-05-13 22:50:03.000000000 -0400
+++ /Users/mradcliffe/Desktop/userpoints_ubercart/uc_userpoints_payment.module	2009-04-28 05:56:21.000000000 -0400
@@ -1,10 +1,11 @@
 <?php
 
-//$Id: uc_userpoints_payment.module,v 1.6.2.1 2009/05/14 02:47:13 smokris Exp $
+//$Id: uc_userpoints_payment.module,v 1.5 2008/07/27 23:05:40 bmagistro Exp $
 
 define('USERPOINTS_UC_PAYMENT',   'userpoints_ubercart_payment');
 define('USERPOINTS_DISPLAY',	    'userpoints_display');
 define('USERPOINTS_PAY_MODERATE',	'userpoints_pay_moderate');
+define('USERPOINTS_PAY_CATEGORY',		'userpoints_PAY_category');
 
 function uc_userpoints_payment_help($path, $arg) {
   switch ($path) {
@@ -49,6 +50,14 @@
           '#default_value' => variable_get(USERPOINTS_PAY_MODERATE, 0),
 					'#options' => array(t('No'), t('Yes')),
         );
+        $form[$group][USERPOINTS_PAY_CATEGORY] = array(
+          '#type' => 'select',
+          '#title' => t('Payment Category'),
+          '#default_value' => variable_get(USERPOINTS_PAY_CATEGORY, NULL),
+          '#options' =>  userpoints_get_categories(),
+          '#description' => t('Category to assign awarded !points.', userpoints_translation()),
+        );
+
       }
       return $form;
       break;
@@ -66,8 +75,15 @@
 			$dispStyle = variable_get(USERPOINTS_DISPLAY,2);
 			//disp 1=dollars, 2=both, 3=points
 	
+			if(module_exists("uc_userpoints_product")) {
+                $points = db_result(db_query("SELECT points FROM {uc_userpoints_products} WHERE nid = %d", $node->nid));
+                if ($points > 0) {
+                    $dispStyle = 1;  // you can't buy a point product with points
+                }
+			}
+
 			$pointExchange = intval(variable_get(USERPOINTS_UC_PAYMENT, 1));
-			$pointCost = intval($node->sell_price * $pointExchange);
+			$pointCost = intval(round($node->sell_price) * $pointExchange);
 			switch($dispStyle) {
 				case 1: 
 					//do nothing since we only want dollars
@@ -75,11 +91,11 @@
 					//$node->content['display_price']['#value'] = '$$';
 					break;
 				case 2: 
-					$node->content['sell_price']['#value'] .= t('!Points: ' . $pointCost, userpoints_translation());
+					$node->content['sell_price']['#value'] .= '<div class="sell_price">' . t('!Points: ' . $pointCost, userpoints_translation()). '</div>';
 					$node->content['display_price']['#value'] .= '<div class="display_price">' . t('!Points: ' . $pointCost, userpoints_translation()) . '</div>';
 					break;
 				case 3: 
-					$node->content['sell_price']['#value'] = t('!Points: ' . $pointCost, userpoints_translation());
+					$node->content['sell_price']['#value'] = '<div class="sell_price">' . t('!Points: ' . $pointCost, userpoints_translation()) . '</div>';
 					$node->content['display_price']['#value'] = '<div class="display_price">' . t('!Points: ' . $pointCost, userpoints_translation()) . '</div>';
 					break;
 			}
@@ -97,8 +113,8 @@
   $curUserId = $user->uid;
 
   //how many points does this user have?
-  $curUserPoints = userpoints_get_current_points($uid = $curUserId, $tid = NULL);
-  $maxdisc = $curUserPoints / intval(variable_get(USERPOINTS_UC_PAYMENT, 1));
+  $curUserPoints = userpoints_get_current_points($uid = $curUserId, $tid = variable_get(USERPOINTS_PAY_CATEGORY, NULL));
+  $maxdisc = round($curUserPoints / intval(variable_get(USERPOINTS_UC_PAYMENT, 1)));
 
   // make sure they have points in the first place
   if ($curUserPoints > 0 || request_uri() != '/cart/checkout') {
@@ -157,7 +173,7 @@
 function uc_payment_method_points($op, &$arg1) {
   global $user;
   $curUserId	 = $user->uid;
-  $curUserPoints = intval(userpoints_get_current_points($uid = $curUserId, $tid = NULL));
+  $curUserPoints = intval(userpoints_get_current_points($uid = $curUserId, $tid = variable_get(USERPOINTS_PAY_CATEGORY, NULL)));
   $pointExchange = intval(variable_get(USERPOINTS_UC_PAYMENT, 1));
 
   switch ($op) {
@@ -172,31 +188,24 @@
 			// check thier points and make sure they have enough to pay for this purchase
 			//order information
 			$order_id 	  = $arg1->order_id;
-			
-			$items = uc_cart_get_contents();
-			foreach ($items as $cartitem) {
-			  $cartbalance += $cartitem->price*$cartitem->qty;
-			}
-			$shipresult		= db_query("select amount from {uc_order_line_items} where `type`='shipping'and  order_id = %d", $order_id);
-			$shipitem 	  = db_fetch_array($shipresult);
-			$orderTotal   = $shipitem['amount']+$cartbalance;
-			$pointsNeeded = intval(($orderTotal * $pointExchange));
+			$result	  	  = db_query("select order_total from {uc_orders} where order_id = %d", $order_id);
+			$item 	  	  = db_fetch_array($result);
+			$orderTotal   = $item['order_total'];
+			$pointsNeeded = intval((round($orderTotal) * $pointExchange));
 
 			if (($curUserPoints < $pointsNeeded && $curUserId != 0)) {
 				drupal_set_message('You do not have enough points to complete this purchase.  You have ' . $curUserPoints . ' but you need ' . $pointsNeeded . '.  Please select another payment method.', 'error');
 				return FALSE;
-				break;
 			}
 			else
 			{
 				return TRUE;
-				break;
 			}
 			break;
 		case 'settings':
 			// Add a few points related fields to the payment methods settings form.
 			$form['userpoints_ubercart_permission'] = array(
-				'#value' => '<div>'. t('Please adjust the settings for using points under the userpoints section '.l('userpoints admin settings','admin/settings/userpoints').'.  This area is for enabling points as payment method only.') .'</div>',
+				'#value' => '<div>'. t('Please adjust the settings for using points under the '.l('userpoints admin settings','admin/settings/userpoints').'.  This area is for enabling points as payment method only.') .'</div>',
 		);
 			return $form;
 			break;
@@ -208,7 +217,7 @@
   $curUserId	= $user->uid;
   $pointExch	= intval(variable_get(USERPOINTS_UC_PAYMENT, 1));
   $orderTotal	= $order->order_total;
-  $points		= intval((($orderTotal * $pointExch) * (-1)));
+  $points		= intval(((round($orderTotal) * $pointExch) * (-1)));
   $description	= 'User made a purchase and paid with points (Ubercart Order ' . $order->order_id . ')';
 
   if ($order === FALSE || uc_order_status_data($order->order_status, 'state') != 'in_checkout') {
@@ -218,7 +227,7 @@
 
   if ($order->payment_method == 'points') {  
 		$params = array (
-			'tid' => 0,
+			'tid' => variable_get(USERPOINTS_PAY_CATEGORY, NULL),
 			'uid' => $curUserId,
 			'points' => $points,
 			'operation' => 'delete',
@@ -230,7 +239,7 @@
 		userpoints_userpointsapi($params);
 		uc_payment_enter($order->order_id, 'points', $orderTotal, $curUserId, NULL, $description);
 		db_query("insert into {uc_up_payment_log} (uid, oid, points) values(%d, %d, %d)",$curUserId, $order->order_id, $points);
-		uc_order_update_status($order->order_id, 'payment_received');
+		uc_order_update_status($order_id, 'payment_received');
   }
 }
 
@@ -250,7 +259,7 @@
 		db_query("delete from {uc_up_payment_log} where uplog_id = %d", $pointinfo->uplog_id);
 		
 		$params = array (
-			'tid' => 0,
+			'tid' => variable_get(USERPOINTS_PAY_CATEGORY, NULL),
 			'uid' => $curUserId,
 			'points' => $points,
 			'operation' => 'insert',
diff -u -b userpoints_ubercart/uc_userpoints_product.module /Users/mradcliffe/Desktop/userpoints_ubercart/uc_userpoints_product.module
--- userpoints_ubercart/uc_userpoints_product.module	2009-05-13 22:50:03.000000000 -0400
+++ /Users/mradcliffe/Desktop/userpoints_ubercart/uc_userpoints_product.module	2009-04-27 06:57:44.000000000 -0400
@@ -1,5 +1,51 @@
 <?php
 /* major contributions and development by torgosPizza */
+define('USERPOINTS_PRODUCT_MODERATE',		'userpoints_product_moderate');
+define('USERPOINTS_PRODUCT_CATEGORY',		'userpoints_product_category');
+
+
+function uc_userpoints_product_help($path, $arg) {
+  switch ($path) {
+    case 'admin/modules#description':
+      $output = t('<strong>Ubercart Userpoints Product:</strong> Interfaces userpoints with Ubercart, so users can purchase points with products.');
+      break;
+  }
+  return $output;
+}
+
+
+function uc_userpoints_product_userpoints($op, $params = array()) {
+  switch($op) {
+    case 'setting':
+      if (module_exists('uc_cart') && module_exists('uc_payment')) {
+        $group = 'uc_userpoints_product';
+        $form[$group] = array(
+          '#type' => 'fieldset',
+          '#collapsible' => TRUE,
+          '#collapsed' => TRUE,
+          '#title' => t('Ubercart Product Options'),
+        );
+        $form[$group][USERPOINTS_PRODUCT_MODERATE] = array(
+          '#type' => 'radios',
+          '#title' => t('Moderate userpoints transaction'),
+          '#default_value' => variable_get(USERPOINTS_PRODUCT_MODERATE, 0),
+					'#options' => array(t('No'), t('Yes')),
+        );
+        $form[$group][USERPOINTS_PRODUCT_CATEGORY] = array(
+          '#type' => 'select',
+          '#title' => t('Product Category'),
+          '#default_value' => variable_get(USERPOINTS_PRODUCT_CATEGORY, NULL),
+          '#options' =>  userpoints_get_categories(),
+          '#description' => t('Category to assign awarded !points.', userpoints_translation()),
+        );
+
+      }
+      return $form;
+      break;
+  }
+}
+
+
 /**
 * Implementation of hook_product_feature().
 */
@@ -9,7 +55,7 @@
 		'title' => t(variable_get(USERPOINTS_TRANS_UCPOINTS, 'Userpoints')),
 		'callback' => 'uc_userpoints_product_feature_form',
 		'delete' => 'uc_userpoints_product_feature_delete',
-		'settings' => array('uc_userpoints_product_feature_settings'),
+		'settings' => 'uc_userpoints_product_feature_settings',
 	);
 	return $features;
 }
@@ -17,64 +63,80 @@
 /**
 * Build the userpoints feature form
 */
-function uc_userpoints_product_feature_form($node, $feature) {
-	$points = 0;
-
-	$nid = arg(1);
-
-	$res = db_query("SELECT * FROM {uc_userpoints_products} ucup, {uc_product_features} ucpf WHERE ucup.pfid = ucpf.pfid AND ucup.nid = %d",$nid);
-	while ($pf = db_fetch_object($res)) {
-	  $points = $pf->points;
-	}
-
+function uc_userpoints_product_feature_form(&$form_state, $node, $feature) {
 	$form['nid'] = array(
 		'#type' => 'value',
-		'#value' => $nid,
+		'#value' => $node->nid,
 	);
+    $form['pfid'] = array(
+        '#type' => 'value',
+        '#value' => $feature['pfid'],
+    );
+
+	// Use the old value to fill the form, if it exists
+	if (isset($feature['pfid'])) {
+	   $points = db_result(db_query("SELECT points FROM {uc_userpoints_products} WHERE nid = %d AND pfid = %d", $feature['nid'], $feature['pfid']));
+	}
+
 	$form['points'] = array(
 		'#type' => 'textfield',
 		'#title' => t('Number of '.variable_get(USERPOINTS_TRANS_LCPOINTS, 'points')),
 		'#description' => t('How many '.variable_get(USERPOINTS_TRANS_LCPOINTS, 'points').' to give when this product is purchased.'),
 		'#default_value' => $points,
 	);
+
 	return uc_product_feature_form($form);
 }
 
-
+/**
+ * Validate points
+ */
 function uc_userpoints_product_feature_form_validate($form, &$form_state) {
-  if (!is_numeric($form_state['values']['points'])) {
-    form_set_error('points','You must enter a numeric value for this field.');
+    if (!$form_state['values']['points'] || !is_numeric($form_state['values']['points']) || $form_state['values']['points'] < 1) {
+        form_set_error('points', t('The number of points must be a positive, non zero number'));
   }
 }
 
-
 /**
 * Submit the form
 */
 function uc_userpoints_product_feature_form_submit($form, &$form_state) {
 	$data = array(
-		'pfid' => $form_state['values']['pfid'],
 		'nid' => $form_state['values']['nid'],
+	    'pfid' => $form_state['values']['pfid'],
 		'fid' => 'userpoints',
 		'description' => 'Product is now worth '.$form_state['values']['points'].' '.variable_get(USERPOINTS_TRANS_LCPOINTS, 'points'),
 	);
 	
-	$ret = uc_product_feature_save($data);
-	//Note: Ubercart Developers are the worst. This function should really pass $data as a reference so I can reference the newly created pfid. UGH! See: http://drupal.org/node/434676
-	if (empty($data['pfid'])) {
-		$pf = db_fetch_object(db_query("select pfid from {uc_userpoints_products} where nid = %d and fid = %d",$data['nid'],$data['fid']));
-		$data['pfid'] = $pf->pfid;
+	$userpoints_product = array(
+        'nid' => $form_state['values']['nid'],
+        'pfid' => $form_state['values']['pfid'],
+        'points' => $form_state['values']['points'],
+	);
+
+	// Save the feature
+    $form_state['redirect'] = uc_product_feature_save($data);
 
-		db_query("INSERT INTO {uc_userpoints_products} (pfid,nid,points) VALUES (%d,%d,%d)",$data['pfid'],$data['nid'],$form_state['values']['points']);
+    // Retrive pfid if feature is new
+    if(empty($userpoints_product['pfid'])) {
+        $userpoints_product['pfid'] = db_last_insert_id('{uc_product_features}','pfid');
 	}
-	else {
-		db_query("UPDATE {uc_userpoints_products} SET points = %d WHERE nid = %d AND pfid = %d",$form_state['values']['points'],$data['nid'],$data['pfid']);
+
+    $key = NULL;
+    if ($upid = _uc_userpoints_get_upid($userpoints_product['pfid'])) {
+        $key = 'upid';
+        $userpoints_product['upid'] = $upid;
 	}
 
-	$form_state['redirect'] = $ret;
+    // Save userpoints value
+    drupal_write_record('uc_userpoints_products',$userpoints_product, $key);
 }
 
 
+function _uc_userpoints_get_upid($pfid) {
+    return db_result(db_query("SELECT upid FROM {uc_userpoints_products} WHERE pfid = %d", $pfid));
+}
+
 /**
  * Feature settings
  */
@@ -88,7 +150,6 @@
  */
 function uc_userpoints_product_feature_delete($feature) {
 		db_query("DELETE FROM {uc_userpoints_products} WHERE pfid = %d",$feature['pfid']);
-		db_query("DELETE FROM {uc_product_features} WHERE pfid = %d", $feature['pfid']);
 }
 
 
@@ -101,26 +162,28 @@
 		case 'update':
 		// We might want to add the "Order Status" to the Userpoints Ubercart options...
 			if (($status == 'completed' && $order->uid > 0) && ($order_user = user_load(array('uid' => $order->uid))) !== FALSE) {
+                $totalPoints = 0;
 				foreach ($order->products as $product) {
-					$points = db_result(db_query("SELECT points FROM {uc_userpoints_products} WHERE `nid`=%d", $product->nid));
+                    $points = db_result(db_query("SELECT SUM(points) FROM {uc_userpoints_products} WHERE nid = %d", $product->nid));
 					if ($points > 0) {
 						//drupal_set_message($product->title.' has '.$points.' '.variable_get(USERPOINTS_TRANS_LCPOINTS, 'points').' attached');
-						$totalPoints .= ($product->qty * $points); // Make sure we account for multiple qty of the Points product
+                        $totalPoints += ($product->qty * $points); // Make sure we account for multiple qty of the Points product
 					}
 				}
 
 			$params = array (
-				'tid' => 0,
+				'tid' => variable_get(USERPOINTS_PRODUCT_CATEGORY, NULL),
 				'uid' => $order->uid,
 				'points' => $totalPoints,
 				'operation' => 'add',
-				'description' => t('A user purchased a product containing points for a total of ' .$totalPoints.' '. variable_get(USERPOINTS_TRANS_LCPOINTS, 'points')),
+				'description' => t('The user purchased a product containing points for a total of ' .$totalPoints.' '. variable_get(USERPOINTS_TRANS_LCPOINTS, 'points')),
 				'entity_id' => $order->oid,
-				'entity_type' => 'Ubercart Transaction'
+				'entity_type' => 'Ubercart Transaction',
+				'moderate' => variable_get(USERPOINTS_PRODUCT_MODERATE, 0),
 			);
 
 		 userpoints_userpointsapi($params);
-		 uc_order_comment_save($order->order_id, 0, t('User earned '.$totalPoints.' points!'), 'admin');
+                uc_order_comment_save($order->order_id, $order->uid, t('User earned '.$totalPoints.' points!'), 'admin');
 		}
 	break;
 	}
@@ -130,7 +193,7 @@
 * Check and make sure the user isn't trying to buy points with points.
 *  @todo: Make this configurable?
 */
-function uc_userpoints_product_form_alter(&$form, $form_state, $form_id) {
+function uc_userpoints_product_form_alter(&$form, &$form_state, $form_id) {
   // We may need to alter the checkout form to remove invalid payment methods.
   if ($form_id == 'uc_cart_checkout_form' && isset($form['panes']['payment'])) {
            
@@ -139,9 +202,11 @@
     // Make no changes if no products containing Userpoints are found.
     if (uc_userpoints_find_points_products($order) == array()) {
       return;
-    } else {
+    }
+
+    if (isset($form['panes']['payment']['payment_method']['#options']['points'])){
             unset($form['panes']['payment']['payment_method']['#options']['points']);
-            drupal_set_message(t('You cannot purchase !Points with !Points, so that payment option has been removed.', userpoints_translation()),'error');
+      drupal_set_message(t('You cannot purchase !Points with !Points, so that payment option has been removed.', userpoints_translation()));
         }
        
     $count = count($form['panes']['payment']['payment_method']['#options']);
@@ -177,7 +242,7 @@
   }
 
   $pointsproducts = array();
-  $result = db_query("SELECT * FROM {uc_userpoints_products} WHERE nid IN ('". implode("', '", $nids) ."')");
+  $result = db_query("SELECT * FROM {uc_userpoints_products} WHERE nid IN (". implode(", ", $nids) .")");
   while ($pntproduct = db_fetch_object($result)) {
     $pointsproducts[] = $pntproduct;
         //dsm('Matched nid: '.$pntproduct->nid);
