diff --git a/apachesolr_ubercart.module b/apachesolr_ubercart.module
index 161bdac..8f148cb 100644
--- a/apachesolr_ubercart.module
+++ b/apachesolr_ubercart.module
@@ -8,27 +8,51 @@
 function apachesolr_ubercart_apachesolr_update_index(&$document, $node) {
   if (in_array($node->type, module_invoke_all('product_types'))) {
 
-    if(!empty($node->model)) {
+    if (!empty($node->model)) {
       $document->ss_uc_sku = $node->model;
     }
     if (!empty($node->list_price)) {
-      $document->fs_uc_list_price = $node->list_price;
+      $document->fs_uc_list_price = uc_price($node->list_price, array('revision' => 'altered', 'type' => $node->type, 'subject' => array('node' => $node)));
     }
     if (!empty($node->sell_price)) {
-      $document->fs_uc_sell_price = $node->sell_price;
+      $document->fs_uc_sell_price = uc_price($node->sell_price, array('revision' => 'altered', 'type' => $node->type, 'subject' => array('node' => $node)));
     }
-    if(!empty($node->weight)) {
+    if (!empty($node->weight)) {
       $document->fs_uc_weight = $node->weight;
     }
-    if(!empty($node->length)) {
+    if (!empty($node->length)) {
       $document->fs_uc_length = $node->length;
     }
-    if(!empty($node->width)) {
+    if (!empty($node->width)) {
       $document->fs_uc_width = $node->width;
     }
-    if(!empty($node->height)) {
+    if (!empty($node->height)) {
       $document->fs_uc_height = $node->height;
     }
+    if (!empty($node->ordening)) {
+      $document->fs_uc_position = $node->ordering;
+    }
+
+    //index also how many times a product has been sold
+    $stat = db_result(db_query('SELECT SUM(qty) FROM {uc_order_products} WHERE nid = %d', $node->nid));
+    //to prevent Apache Solr Errors while indexing empty values
+    if (empty($stat)) {
+      $stat = 0;
+    }
+    $document->fs_common_num_sold = $stat;
+
+    //attributes
+    if (!empty($node->attributes)) {
+      foreach ($node->attributes as $attribute) {
+        $solr_index_name ="sm_uc_attribute_".$attribute->aid;
+        foreach ($attribute->options as $options) {
+          //we could choose to use oid or to use string
+          //for max maintainabilityand "hookability" we go for oid
+          //we will use theming functions to output decent names
+          $document->setMultiValue($solr_index_name, $options->oid);
+        }
+      }
+    }
   }
 }
 
@@ -72,6 +96,21 @@ function apachesolr_ubercart_apachesolr_facets() {
     'facet_field' => 'fs_uc_height',
     'dividable' => TRUE,
   );
+  if (module_exists('uc_attributes')) {
+    $types = uc_product_types();
+    foreach ($types as $type_id => $type) {
+      $attributes = apachesolr_ubercart_uc_get_attributes();
+
+      foreach ($attributes as $attribute) {
+        $solr_index_name ="sm_uc_attribute_".$attribute->aid;
+        $facets[$solr_index_name] = array(
+          'info' => t('Apache Solr Search: Filter by attribute @attribute', array('@attribute' => $attribute->name)),
+          'facet_field' => $solr_index_name,
+        );
+      }
+    }
+  }
+
   return $facets;
 }
 
@@ -96,20 +135,26 @@ function apachesolr_ubercart_block($op = 'list', $delta = 0, $edit = array()) {
       $facets = apachesolr_ubercart_apachesolr_facets();
       $form = array();
       if (array_key_exists($delta, $facets)) {
-        if($facets[$delta]['dividable']) {
-         $form['apachesolr_ubercart_division'] = array(
-           '#type' => 'textfield',
-           '#title' => t('The division of the price block (split all our prices by 20 for example'),
-           '#default_value' => variable_get('apachesolr_ubercart_division_'.$delta,20),
-         );
+        if ($facets[$delta]['dividable']) {
+          $form['apachesolr_ubercart_division'] = array(
+            '#type' => 'textfield',
+            '#title' => t('The division of the price block (split all our prices by 20 for example'),
+            '#default_value' => variable_get('apachesolr_ubercart_division_'.$delta,20),
+          );
+          $form['apachesolr_ubercart_automatic_division'] = array(
+            '#type' => 'checkbox',
+            '#title' => t('You can also choose to let Apachesolr Ubercart divide the prices for you. This is enabled by default'),
+            '#default_value' => variable_get('apachesolr_ubercart_automatic_division_'.$delta,TRUE),
+          );
         }
       }
       return $form;
     case 'save':
       $facets = apachesolr_ubercart_apachesolr_facets();
-      if(array_key_exists($delta,$facets)) {
-        if($facets[$delta]['dividable']) {
+      if (array_key_exists($delta,$facets)) {
+        if ($facets[$delta]['dividable']) {
           variable_set('apachesolr_ubercart_division_'.$delta, (int) $edit['apachesolr_ubercart_division']);
+          variable_set('apachesolr_ubercart_automatic_division_'.$delta, (int) $edit['apachesolr_ubercart_automatic_division']);
         }
       }
       break;
@@ -127,28 +172,41 @@ function apachesolr_ubercart_block($op = 'list', $delta = 0, $edit = array()) {
         }
 
         // Get information needed by the taxonomy blocks about limits.
-        $initial_limits = variable_get('apachesolr_facet_query_initial_limits', array());
-        $limit_default = variable_get('apachesolr_facet_query_initial_limit_default', 10);
-        $division = variable_get('apachesolr_apachesolr_ubercart_division_' . $delta, 20);
+        $params = array();
+        $params['initial_limits'] = variable_get('apachesolr_facet_query_initial_limits', array());
+        $params['division'] = variable_get('apachesolr_ubercart_division_' . $delta, 20);
+        $params['automatic_division'] = variable_get('apachesolr_ubercart_automatic_division_' . $delta, TRUE);
+        $params['limit_default'] = variable_get('apachesolr_facet_query_initial_limit_default', 10);
+
         switch ($delta) {
           case 'fs_uc_sell_price':
-            return apachesolr_ubercart_price_facet_block($response, $query, 'apachesolr_ubercart', $delta, $delta, t('Filter by price'),$division);
+            return apachesolr_ubercart_price_facet_block($response, $query, 'apachesolr_ubercart', $delta, $delta, t('Filter by Sell Price'),$params);
           case 'fs_uc_list_price':
-            return apachesolr_ubercart_price_facet_block($response, $query, 'apachesolr_ubercart', $delta, $delta, t('Filter by price'),$division);
+            return apachesolr_ubercart_price_facet_block($response, $query, 'apachesolr_ubercart', $delta, $delta, t('Filter by List Price'),$params);
           case 'ss_uc_sku':
             return apachesolr_facet_block($response, $query, 'apachesolr_ubercart', $delta, $delta, t('Filter by product ID'));
           case 'fs_uc_weight':
-            return apachesolr_ubercart_price_facet_block($response, $query, 'apachesolr_ubercart', $delta, $delta, t('Filter by weight'),$division);
+            return apachesolr_ubercart_price_facet_block($response, $query, 'apachesolr_ubercart', $delta, $delta, t('Filter by weight'),$params);
           case 'fs_uc_length':
-            return apachesolr_ubercart_price_facet_block($response, $query, 'apachesolr_ubercart', $delta, $delta, t('Filter by length'),$division);
+            return apachesolr_ubercart_price_facet_block($response, $query, 'apachesolr_ubercart', $delta, $delta, t('Filter by length'),$params);
           case 'fs_uc_width':
-            return apachesolr_ubercart_price_facet_block($response, $query, 'apachesolr_ubercart', $delta, $delta, t('Filter by width'),$division);
+            return apachesolr_ubercart_price_facet_block($response, $query, 'apachesolr_ubercart', $delta, $delta, t('Filter by width'),$params);
           case 'fs_uc_height':
-            return apachesolr_ubercart_price_facet_block($response, $query, 'apachesolr_ubercart', $delta, $delta, t('Filter by height'),$division);
+            return apachesolr_ubercart_price_facet_block($response, $query, 'apachesolr_ubercart', $delta, $delta, t('Filter by height'),$params);
+        }
+
+        $types = uc_product_types();
+        foreach($types as $type_id => $type) {
+          $attributes = apachesolr_ubercart_uc_get_attributes();
+          foreach($attributes as $attribute) {
+            $solr_index_name ="sm_uc_attribute_".$attribute->aid;
+            if($delta == $solr_index_name) {
+              return apachesolr_facet_block($response, $query, 'apachesolr_ubercart', $delta, $delta, t('Filter by attribute @attribute', array('@attribute' => $attribute->name)), 'apachesolr_ubercart_attribute_to_name');
+            }
+          }
         }
-        break;
       }
-      break;
+    break;
   }
 }
 
@@ -159,39 +217,85 @@ function apachesolr_ubercart_block($op = 'list', $delta = 0, $edit = array()) {
  * We should reuse more code, that means apachesolr_facet_block() should be
  * properly abstracted.
  */
-function apachesolr_ubercart_price_facet_block($response, $query, $module, $delta, $facet_field, $filter_by, $division, $facet_callback = FALSE) {
-
+function apachesolr_ubercart_price_facet_block($response, $query, $module, $delta, $facet_field, $filter_by, $params, $facet_callback = FALSE) {
   if (!empty($response->facet_counts->facet_fields->$facet_field)) {
 
+    $facet_query_sorts = variable_get('apachesolr_facet_query_sorts', array());
     $contains_active = FALSE;
     $items = array();
-
     // Construct our facet link amounts.
-    if(!empty($response->facet_counts->facet_fields->$delta)) {
-      $links = array();
-      foreach($response->facet_counts->facet_fields->$delta as $price => $count){
+    $links = array();
+    $min_price = 9999999999999999;
+    $max_price = 0;
 
-        // we use Intval to filter on our divisions.
-        $divide  = intval($price/$division);
-        $links[$divide] += $count ;
+    //loop through our results once to find out the min and max price
+    foreach ($response->facet_counts->facet_fields->$delta as $price => $count){
+      //calculate dynamic division
+      if ($price > $max_price) {
+        $max_price = $price;
+      }
+      if ($price < $min_price) {
+        $min_price = $price;
       }
     }
 
+    //checking for automatic_division
+    if (isset($params['automatic_division']) && ($params['automatic_division'] == TRUE) && ($max_price != 0)) {
+      //We want no more then 5 facets available in our block
+      if($max_price == $min_price) {
+        $min_price = 0;
+      }
+      $division = intval(($max_price-$min_price)/5) == 0 ? 1 : intval(($max_price-$min_price)/5);
+      if($division == 0) {
+        $division = $max_price/2;
+      }
+    }
+    else {
+      $division = $params['division'];
+    }
+
+    //loop again through the prices with the division in mind
+    foreach ($response->facet_counts->facet_fields->$delta as $price => $count){
+    $options = array('delta' => $delta);
+      $exclude = FALSE;
+
+      // Solr sends this back if it's empty.
+      if ($facet == '_empty_') {
+        $exclude = TRUE;
+        $facet = '[* TO *]';
+        $options['html'] = TRUE;
+      }
+      // we use Intval to filter on our divisions.
+      $divide  = intval($price/$division);
+      $links[$divide] += $count ;
+    }
+
     // Iterate over the available facet links.
-    foreach($links as $price_range => $count) {
+    foreach ($links as $price_range => $count) {
+
       $sortpre = 1000000 - $count;
       $options = array();
       $exclude = FALSE;
-      $from_price = $price_range * $division;
-      $to_price = ($price_range * $division) + $division;
-      $facet_text = t('from @price_from to @price_to',array('@price_from'=>$from_price,'@price_to'=>$to_price));
+      $from_price = ($price_range * $division);
+      $output_from_price = uc_currency_format($from_price, $sign = TRUE, $thou = TRUE, $dec = NULL);
+      //patched from issue #845152 making double facet items impossible
+      $to_price = (($price_range * $division) + $division) - 0.01;
+      $output_to_price = uc_currency_format($to_price, $sign = TRUE, $thou = TRUE, $dec = NULL);
+      $facet_text = t('from @price_from to @price_to',
+        array(
+          '@price_from'=> $output_from_price,
+          '@price_to'=>$output_to_price
+        )
+      );
+      //we do not put this in a t function since this is a solr parameter'
       $facet = '['.$from_price.' TO '.$to_price.']';
       $unclick_link = '';
       $active = FALSE;
       $new_query = clone $query;
+      $active = $query->has_filter($facet_field, $facet);
 
       if ($query->has_filter($facet_field, $facet)) {
-        $contains_active = $active = TRUE;
+        $contains_active = TRUE;
         // '*' sorts before all numbers.
         $sortpre = '*';
         $new_query->remove_filter($facet_field, $facet);
@@ -226,9 +330,112 @@ function apachesolr_ubercart_price_facet_block($response, $query, $module, $delt
  */
 function apachesolr_ubercart_apachesolr_prepare_query(&$query) {
   $query->set_available_sort('fs_uc_sell_price', array(
-    'title' => t('Price'),
+    'title' => t('Sell Price'),
+    'default' => 'asc',
+  ));
+  $query->set_available_sort('fs_uc_list_price', array(
+    'title' => t('List Price'),
+    'default' => 'asc',
+  ));
+  $query->set_available_sort('fs_common_num_sold', array(
+    'title' => t('Sold times'),
     'default' => 'asc',
   ));
+  $query->set_available_sort('fs_uc_weight', array(
+    'title' => t('Weight'),
+    'default' => 'asc',
+  ));
+  $query->set_available_sort('fs_uc_width', array(
+    'title' => t('Width'),
+    'default' => 'asc',
+  ));
+  $query->set_available_sort('fs_uc_height', array(
+    'title' => t('Height'),
+    'default' => 'asc',
+  ));
+  $query->set_available_sort('fs_uc_position', array(
+    'title' => t('Position'),
+    'default' => 'asc',
+  ));
+}
+
+/**
+ * Implementation of hook_ca_action().
+ *
+ * Demonstrates defining an action for predicates to use; primarily specifies a
+ * callback function to perform the action and an array that specifies arguments
+ * and their data types.
+ */
+function apachesolr_ubercart_ca_action() {
+  $actions['apachesolr_ubercart_set_stats_in_node'] = array(
+    '#title' => t('Update the statistics in the node field'),
+    '#category' => t('Statistics'),
+    '#callback' => 'apachesolr_ubercart_update_stats',
+    '#arguments' => array(
+        'order' => array('#entity' => 'uc_order', '#title' => t('Order')),
+      ),
+  );
+  return $actions;
+}
+
+/**
+ * Perform a custom PHP action.
+ *
+ * @see ca_action_custom_php_form()
+ */
+function apachesolr_ubercart_update_stats(&$order, $settings = array()) {
+  //check if we have products
+  if (is_array($order->products)) {
+    foreach ($order->products as $product) {
+      //load node
+      $node = node_load($product->nid);
+      //change the updated date to the current time so it can be reindexed when the next solr cron runs
+      $node->changed = time();
+      //save the node
+      node_save($node);
+    }
+  }
+}
+
+/**
+ * Implementation of hook_ca_predicate().
+ * Definition of our statistics updater
+ */
+function apachesolr_ubercart_ca_predicate() {
+  $predicates = array();
+
+  $predicates['apachesolr_ubercart_update_stats'] = array(
+    '#title' => t('Update statistics'),
+    '#description' => t('Update the statistics for the most sold in Solr by changing the products date.'),
+    '#class' => 'apachesolr_ubercart',
+    '#status' => 1,
+    '#trigger' => 'uc_checkout_complete',
+    '#actions' => array(
+      array(
+        '#name' => 'apachesolr_ubercart_set_stats_in_node',
+        '#title' => t('Update the changed date in the node'),
+        '#argument_map' => array(
+          'order' => 'order',
+        ),
+      ),
+    ),
+  );
+  return $predicates;
+}
+
+
+function apachesolr_ubercart_uc_get_attributes() {
+  $attributes = array();
+  $result = db_query("SELECT ua.aid FROM {uc_attributes} AS ua ORDER BY ua.ordering, ua.name");
+  while ($attribute = db_fetch_object($result)) {
+    $attributes[$attribute->aid] = uc_attribute_load($attribute->aid, $pcid, 'class');
+  }
+  return $attributes;
+}
+
+function apachesolr_ubercart_attribute_to_name($facet, $options) {
+  $option = uc_attribute_option_load($facet);
+  return $option->name;
 }
 
 /**
@@ -247,4 +454,4 @@ function apachesolr_ubercart_theme() {
  */
 function apachesolr_ubercart_views_api() {
   return array('api' => '3.0-dev');
-}
+}
\ No newline at end of file
