diff --git a/commerce_discount.module b/commerce_discount.module
index 795de84..2370dd4 100644
--- a/commerce_discount.module
+++ b/commerce_discount.module
@@ -9,11 +9,21 @@
  */
 
 /**
- * Implements hook_commerce_cart_order_refresh().
+ * Implements hook_commerce_order_presave().
  *
  * Remove any existing commerce discount line item.
  */
-function commerce_discount_commerce_cart_order_refresh($wrapper) {
+function commerce_discount_commerce_order_presave($order) {
+  static $in_presave;
+
+  // Avoid recursion if an order save happens inside this function.
+  if ($in_presave) {
+    return;
+  }
+
+  $in_presave = TRUE;
+
+  $wrapper = entity_metadata_wrapper('commerce_order', $order);
   $line_items_to_delete = array();
   $cloned_line_items = array();
   // Remove all discount references from the order.
@@ -31,8 +41,8 @@ function commerce_discount_commerce_cart_order_refresh($wrapper) {
       $line_items_to_delete[] = $wrapper_line_item->line_item_id->value();
       $wrapper->commerce_line_items->offsetUnset($delta);
     }
-    elseif (is_object($wrapper_line_item) && $line_item = $wrapper_line_item->value()) {
-      $cloned_line_items[$wrapper_line_item->getIdentifier()] = clone $line_item;
+    elseif ($line_item = $wrapper_line_item->value()) {
+      $cloned_line_items[$line_item->line_item_id] = clone $line_item;
 
       // Remove discount components from the line item price fields; the line
       // item may be a discount (so the price will become 0) or another line
@@ -78,6 +88,13 @@ function commerce_discount_commerce_cart_order_refresh($wrapper) {
 
   // Re-add all applicable discount price components and/or line items.
   rules_invoke_event('commerce_discount_order', $wrapper);
+
+  // Recalculate the order total using the current line item data.
+  // Copied from CommerceOrderEntityController::save() because this is run
+  // after that first call.
+  commerce_order_calculate_total($order);
+
+  $in_presave = FALSE;
 }
 
 /**
@@ -968,7 +985,9 @@ function commerce_discount_usage_order_discounts($order) {
   // Add the set of discounts directly referenced on the order.
   if (isset($order->commerce_discounts) && $order_wrapper->commerce_discounts->value()) {
     foreach ($order_wrapper->commerce_discounts->value() as $discount) {
-      $order_discounts[] = $discount->name;
+      if (isset($discount->name)) {
+        $order_discounts[] = $discount->name;
+      }
     }
   }
 
diff --git a/commerce_discount.rules.inc b/commerce_discount.rules.inc
index e76c542..3ea3b34 100644
--- a/commerce_discount.rules.inc
+++ b/commerce_discount.rules.inc
@@ -1339,16 +1339,11 @@ function commerce_discount_free_products(EntityDrupalWrapper $wrapper, $discount
  */
 function commerce_discount_add_line_item(EntityDrupalWrapper $order_wrapper, $discount_name, $discount_amount, $data = array()) {
   // Create a new line item.
-  $values = array(
-    'type' => 'commerce_discount',
-    'order_id' => $order_wrapper->order_id->value(),
-    'quantity' => 1,
-    // Flag the line item.
-    'data' => array('discount_name' => $discount_name) + $data,
-  );
-  $discount_line_item = entity_create('commerce_line_item', $values);
-  $discount_line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $discount_line_item);
-
+  $discount_line_item = commerce_line_item_new('commerce_discount', $order_wrapper->getIdentifier());
+  $discount_line_item->data = array('discount_name' => $discount_name) + $data;
+  // Setting the bundle to ensure the entity metadata stores it correctly.
+  $info = array('bundle' => 'commerce_discount');
+  $discount_line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $discount_line_item, $info);
   // Initialize the line item unit price.
   $discount_line_item_wrapper->commerce_unit_price->amount = 0;
   $discount_line_item_wrapper->commerce_unit_price->currency_code = $discount_amount['currency_code'];
@@ -1365,9 +1360,10 @@ function commerce_discount_add_line_item(EntityDrupalWrapper $order_wrapper, $di
   // Add the discount price component.
   commerce_discount_add_price_component($discount_line_item_wrapper, $discount_name, $discount_amount);
 
-  // Save the line item and add it to the order.
-  $discount_line_item_wrapper->save();
-  $order_wrapper->commerce_line_items[] = $discount_line_item_wrapper;
+  // Save the incoming line item now so we get its ID.
+  commerce_line_item_save($discount_line_item);
+  // Add it to the order's line item reference value.
+  $order_wrapper->commerce_line_items[] = $discount_line_item;
 }
 
 /**
@@ -1407,6 +1403,7 @@ function commerce_discount_add_price_component(EntityDrupalWrapper $line_item_wr
 
   // Set the new unit price.
   $line_item_wrapper->commerce_unit_price->amount = $updated_amount;
+
   // Add the discount amount as a price component.
   $unit_price = commerce_price_wrapper_value($line_item_wrapper, 'commerce_unit_price', TRUE);
   $type = empty($component_title) ? 'discount' : check_plain('discount|' . $discount_name);
@@ -1493,6 +1490,7 @@ function _commerce_discount_update_commerce_total($line_item_wrapper) {
 
   // Set the updated data array to the total price.
   $line_item_wrapper->commerce_total->data = $unit_price['data'];
+  $line_item_wrapper->save();
   // Reset the cache because we aren't saving it.
   entity_get_controller('commerce_line_item')->resetCache(array($line_item_wrapper->getIdentifier()));
 }
diff --git a/tests/commerce_discount.test b/tests/commerce_discount.test
index 5159429..cfebffc 100644
--- a/tests/commerce_discount.test
+++ b/tests/commerce_discount.test
@@ -69,18 +69,20 @@ class CommerceDiscountTest extends CommerceDiscountTestBase {
     $discount = $this->createDiscount('order_discount', 'free_products', array($this->product->product_id));
     // Create a completed order.
     $order = $this->createDummyOrder($this->store_customer->uid, array($this->product->product_id => 1), 'completed');
-    // Recalculate discounts.
-    $order_wrapper = commerce_cart_order_refresh($order);
-    $line_items = $order_wrapper->value()->commerce_line_items[LANGUAGE_NONE];
+    commerce_order_save($order);
+    $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
+
+    $count = count($order->commerce_line_items[LANGUAGE_NONE]);
     $this->assertEqual($order_wrapper->commerce_line_items->count(), count($line_items), 'Number of line items matched');
 
     // Disable the discount.
     $discount->status = FALSE;
     entity_save('commerce_discount', $discount);
+    commerce_order_save($order);
 
-    $order_wrapper = commerce_cart_order_refresh($order);
-    $line_items = $order_wrapper->value()->commerce_line_items[LANGUAGE_NONE];
-    $this->assertEqual($order_wrapper->commerce_line_items->count(), count($line_items), 'Number of line items matched');
+    $count = count($order->commerce_line_items[LANGUAGE_NONE]);
+    $order_wrapper_line_item_count = $order_wrapper->commerce_line_items->count();
+    $this->assertEqual($order_wrapper_line_item_count, $count, 'Number of line items matched.' . order_wrapper_line_item_count . ' == ' . $count);
   }
 
   /**
@@ -93,11 +95,10 @@ class CommerceDiscountTest extends CommerceDiscountTestBase {
 
     // Create an order.
     $order = $this->createDummyOrder($this->store_customer->uid, array($this->product->product_id => 1), 'completed');
-    // Recalculate discounts.
-    $order_wrapper = commerce_cart_order_refresh($order);
+    $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
 
     // Check if the discount was applied on the order total price.
-    $this->assertTrue($order_wrapper->commerce_order_total->amount->value() == 700, 'Fixed order discount is deducted correctly.');
+    $this->assertEqual($order_wrapper->commerce_order_total->amount->value(), 700, 'Fixed order discount is deducted correctly.');
 
     // Disable the discount.
     $discount->status = FALSE;
@@ -105,12 +106,8 @@ class CommerceDiscountTest extends CommerceDiscountTestBase {
 
     // Re-save the order.
     // Check if the discount was applied on the order total price.
-    $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
     $order_wrapper->save();
-
-    // Recalculate discounts.
-    $order_wrapper = commerce_cart_order_refresh($order);
-    $this->assertTrue($order_wrapper->commerce_order_total->amount->value() == 1000, "Fixed order discount is removed when it's not applicable.");
+    $this->assertEqual($order_wrapper->commerce_order_total->amount->value(), 1000, "Fixed order discount is removed when it's not applicable.");
   }
 
   /**
@@ -122,8 +119,7 @@ class CommerceDiscountTest extends CommerceDiscountTestBase {
     $discount = $this->createDiscount('order_discount', 'percentage', 5);
     // Create a completed order.
     $order = $this->createDummyOrder($this->store_customer->uid, array($this->product->product_id => 1), 'completed');
-    // Recalculate discounts.
-    $order_wrapper = commerce_cart_order_refresh($order);
+    $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
 
     // Check if the discount was applied on the order total price.
     $this->assertTrue($order_wrapper->commerce_order_total->amount->value() == 950, 'Percentage order discount is deducted correctly.');
@@ -136,9 +132,6 @@ class CommerceDiscountTest extends CommerceDiscountTestBase {
     // Check if the discount was applied on the order total price.
     $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
     $order_wrapper->save();
-
-    // Recalculate discounts.
-    $order_wrapper = commerce_cart_order_refresh($order);
     $this->assertTrue($order_wrapper->commerce_order_total->amount->value() == 1000, "Percentage order discount is removed when it's not applicable.");
   }
 
@@ -162,17 +155,20 @@ class CommerceDiscountTest extends CommerceDiscountTestBase {
 
     // Create the order and apply the freebie discount.
     $order = $this->createDummyOrder($this->store_admin->uid, array($product->product_id => 1));
-    $order_wrapper = commerce_cart_order_refresh($order);
+    $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
 
+    $line_item = $order_wrapper->commerce_line_items->get(0)->value();
+    rules_invoke_event('commerce_product_calculate_sell_price', $line_item);
+    $order_wrapper->save();
     $properly_applied = $this->discountAppliedToOrder('freebie', $order);
+
+    // @todo Fix test.
     $this->assertTrue($properly_applied, t('100% off discount applied to a product.'));
 
     // Invoke line item price re-calculation.
     $line_item = $order_wrapper->commerce_line_items->get(0)->value();
     rules_invoke_event('commerce_product_calculate_sell_price', $line_item);
 
-    $order_wrapper = commerce_cart_order_refresh($order);
-
     // Verify that the product is now free.
     $line_item_wrapper = $order_wrapper->commerce_line_items->get(0);
     $unit_price = $line_item_wrapper->commerce_unit_price->value();
@@ -189,8 +185,7 @@ class CommerceDiscountTest extends CommerceDiscountTestBase {
     $discount = $this->createDiscount('order_discount', 'free_products', array($this->product->product_id));
     // Create a completed order.
     $order = $this->createDummyOrder($this->store_customer->uid, array($this->product->product_id => 1), 'completed');
-    // Recalculate discounts.
-    $order_wrapper = commerce_cart_order_refresh($order);
+    $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
 
     // Check if the discount was applied on the order total price.
     $this->assertEqual($order_wrapper->commerce_order_total->amount->value(), 1000, 'Free Bonus Products order discount has the price of only one product.');
@@ -202,7 +197,8 @@ class CommerceDiscountTest extends CommerceDiscountTestBase {
 
     // Re-save the order.
     // Check if the discount was applied on the order total price.
-    $order_wrapper = commerce_cart_order_refresh($order);
+    commerce_order_save($order);
+    $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
     $this->assertEqual($order_wrapper->commerce_order_total->amount->value(), 1000, "Free Bonus Products order discount is removed when it's not applicable and price is the same.");
     $this->assertEqual($order_wrapper->commerce_line_items->count(), 1, "Free Bonus Products order discount is removed when it's not applicable and line item count is only 1");
   }
@@ -285,14 +281,18 @@ class CommerceDiscountTest extends CommerceDiscountTestBase {
 
     // Create an order.
     $order = $this->createDummyOrder($this->store_customer->uid, array($this->product->product_id => 1), 'completed');
-    // Recalculate discounts.
-    $order_wrapper = commerce_cart_order_refresh($order);
+    $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
 
     $this->assertTrue($order_wrapper->commerce_discounts->count() == 2, '2 discounts are listed as applied on the order.');
     $this->assertTrue($order_wrapper->commerce_order_total->amount->value() == 500, 'Two fixed order discounts are applied on the total price.');
     $this->assertTrue($order_wrapper->commerce_line_items->count() == 3, 'An order with one product and two fixed order discounts has three line items.');
+
+    // Recalculate discounts.
     $order_wrapper->save();
-    $this->assertTrue($order_wrapper->commerce_line_items->count() == 3, 'After updating the order it still has three line items.');
+
+    entity_get_controller('commerce_order')->resetCache(array($order_wrapper->getIdentifier()));
+    $count = $order_wrapper->commerce_line_items->count();
+    $this->assertEqual($count, 3, 'After updating the order it still has three line items. ' . $count);
   }
 
   /**
@@ -316,10 +316,11 @@ class CommerceDiscountTest extends CommerceDiscountTestBase {
 
     // Create the order and apply discount.
     $order = $this->createDummyOrder($this->store_customer->uid, array($product->product_id => 1), 'completed');
-    $order_wrapper = commerce_cart_order_refresh($order);
+    $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
 
     // Verify rounding came out properly.
     $line_item_wrapper = $order_wrapper->commerce_line_items->get(0);
+    rules_invoke_event('commerce_product_calculate_sell_price', $line_item_wrapper->value());
     $unit_price = $line_item_wrapper->commerce_unit_price->value();
 
     $this->assertEqual($unit_price['amount'], 717, 'Product line item unit price amount rounded properly for a 30% discount.');
@@ -352,8 +353,6 @@ class CommerceDiscountTest extends CommerceDiscountTestBase {
 
     // Test compatibility with both discounts using the "any" strategy. Both
     // discounts should be applied.
-    commerce_cart_order_refresh($order);
-
     $properly_applied = $this->discountAppliedToOrder('of1', $order) && $this->discountAppliedToOrder('of2', $order);
     $this->assertTrue($properly_applied, t('Discount one and two applied when both are compatible with any discount.'));
 
@@ -361,8 +360,9 @@ class CommerceDiscountTest extends CommerceDiscountTestBase {
     // discount one should be applied.
     $discount_one_wrapper->commerce_compatibility_strategy = 'none';
     $discount_one_wrapper->save();
-    commerce_cart_order_refresh($order);
 
+    // Recalculate discounts.
+    commerce_order_save($order);
     $properly_applied = $this->discountAppliedToOrder('of1', $order) && !$this->discountAppliedToOrder('of2', $order);
     $this->assertTrue($properly_applied, t('Only discount one applied when it is not compatible with any other discount.'));
 
@@ -372,8 +372,9 @@ class CommerceDiscountTest extends CommerceDiscountTestBase {
     $discount_one_wrapper->save();
     $discount_two_wrapper->commerce_compatibility_strategy = 'none';
     $discount_two_wrapper->save();
-    commerce_cart_order_refresh($order);
 
+    // Recalculate discounts.
+    commerce_order_save($order);
     $properly_applied = $this->discountAppliedToOrder('of1', $order) && !$this->discountAppliedToOrder('of2', $order);
     $this->assertTrue($properly_applied, t('Only discount one applied when discount two is not compatible with any other discount.'));
 
@@ -384,8 +385,9 @@ class CommerceDiscountTest extends CommerceDiscountTestBase {
     $discount_one_wrapper->save();
     $discount_two_wrapper->commerce_compatibility_strategy = 'any';
     $discount_two_wrapper->save();
-    commerce_cart_order_refresh($order);
 
+    // Recalculate discounts.
+    commerce_order_save($order);
     $properly_applied = $this->discountAppliedToOrder('of1', $order) && !$this->discountAppliedToOrder('of2', $order);
     $this->assertTrue($properly_applied, t('Only discount one applied when it is compatible with any discount except discount two.'));
 
@@ -396,8 +398,9 @@ class CommerceDiscountTest extends CommerceDiscountTestBase {
     $discount_two_wrapper->commerce_compatibility_strategy = 'except';
     $discount_two_wrapper->commerce_compatibility_selection = array($discount_one->discount_id);
     $discount_two_wrapper->save();
-    commerce_cart_order_refresh($order);
 
+    // Recalculate discounts.
+    commerce_order_save($order);
     $properly_applied = $this->discountAppliedToOrder('of1', $order) && !$this->discountAppliedToOrder('of2', $order);
     $this->assertTrue($properly_applied, t('Only discount one applied when it is compatible with any discount and discount two is compatible with any discount except discount one.'));
 
@@ -405,8 +408,9 @@ class CommerceDiscountTest extends CommerceDiscountTestBase {
     // one. Both discounts should be applied.
     $discount_two_wrapper->commerce_compatibility_strategy = 'only';
     $discount_two_wrapper->save();
-    commerce_cart_order_refresh($order);
 
+    // Recalculate discounts.
+    commerce_order_save($order);
     $properly_applied = $this->discountAppliedToOrder('of1', $order) && $this->discountAppliedToOrder('of2', $order);
     $this->assertTrue($properly_applied, t('Both discounts applied when discount one is compatible with any discount and discount two is compatible only with discount one.'));
 
@@ -416,8 +420,9 @@ class CommerceDiscountTest extends CommerceDiscountTestBase {
     $discount_one_wrapper->save();
     $discount_two_wrapper->commerce_compatibility_strategy = 'any';
     $discount_two_wrapper->save();
-    commerce_cart_order_refresh($order);
 
+    // Recalculate discounts.
+    commerce_order_save($order);
     $properly_applied = $this->discountAppliedToOrder('of1', $order) && $this->discountAppliedToOrder('of2', $order);
     $this->assertTrue($properly_applied, t('Both discounts applied when discount one is only compatible with discount two and discount two is compatible with any discount.'));
   }
@@ -435,8 +440,7 @@ class CommerceDiscountTest extends CommerceDiscountTestBase {
 
     // Create an order.
     $order = $this->createDummyOrder($this->store_customer->uid, array($this->product->product_id => 1), 'completed');
-    // Recalculate discounts.
-    $order_wrapper = commerce_cart_order_refresh($order);
+    $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
 
     // Check if the discount was applied on the order total price.
     $this->assertTrue($order_wrapper->commerce_order_total->amount->value() == 700, 'Fixed order discount is deducted correctly.');
@@ -445,8 +449,9 @@ class CommerceDiscountTest extends CommerceDiscountTestBase {
     $discount->delete();
 
     // Recalculate discounts.
-    $order_wrapper = commerce_cart_order_refresh($order);
-    $this->assertTrue($order_wrapper->commerce_order_total->amount->value() == 1000, "Fixed order discount is removed when it's not applicable.");
+    $order_wrapper->save();
+    $total_value = $order_wrapper->commerce_order_total->amount->value();
+    $this->assertEqual($total_value, 1000, "Fixed order discount is removed when it's not applicable. " . $total_value);
   }
 
 }
