diff --git a/user_visits_adv.info b/user_visits_adv.info
index fd06236..8ecedb5 100644
--- a/user_visits_adv.info
+++ b/user_visits_adv.info
@@ -1,4 +1,6 @@
 name = User Visits Advanced
 description = Extends the user_visits module with history data.
 dependencies[] = user_visits
-core = 6.x
\ No newline at end of file
+core = 7.x
+files[] = user_visits_adv.module
+files[] = user_visits_adv.install
diff --git a/user_visits_adv.install b/user_visits_adv.install
index 38d5e9c..938b3a9 100644
--- a/user_visits_adv.install
+++ b/user_visits_adv.install
@@ -1,29 +1,36 @@
 <?php
+// $Id: user_visits_adv.install,v 1.3 2009/03/10 15:58:43 toemaz Exp $
 
 /**
- * Implementation of hook_scema().
+ * @file
+ * user_visits_adv install file
+ */
+
+/**
+ * Implementation of hook_schema().
  */
 function user_visits_adv_schema() {
   $schema['user_visits_adv'] = array(
-    'description' => t('The base table user activity.'),
+    'description' => ('The base table user activity.'),
     'fields' => array(
       'uid' => array(
-        'description' => t('The primary identifier for a user.'),
+        'description' => ('The primary identifier for a user.'),
         'type' => 'int',
         'unsigned' => TRUE,
         'not null' => TRUE),
       'total' => array(
-        'description' => t('Total count for visits.'),
+        'description' => ('Total count for visits.'),
         'type' => 'int',
         'unsigned' => TRUE,
         'not null' => TRUE),
       'visitors' => array(
-        'description' => t('The primary identifier for a visitor.'),
+        'description' => ('The primary identifier for a visitor.'),
         'type' => 'text'),
       'history' => array(
-        'description' => t('History count of the user.'),
+        'description' => ('History count of the user.'),
         'type' => 'varchar',
-        'length' => 255),
+        'length' => 255,
+        'not null' => TRUE),
       ),
     'indexes' => array(
       'uid'  => array('uid'),
@@ -34,19 +41,20 @@ function user_visits_adv_schema() {
 
 /**
  * Implementation of hook_install().
- */
+
 function user_visits_adv_install() {
   drupal_install_schema('user_visits_adv');
 }
-
+*/
 /**
  * Implementation of hook_uninstall().
- */
+
 function user_visits_adv_uninstall() {
-  drupal_uninstall_schema('user_visits_adv');
+  db_query('DROP TABLE {user_visits_adv}');
 
   variable_del('user_visits_adv_history_limit');
   variable_del('user_visits_adv_visitor_limit');
   variable_del('user_visits_adv_block_items_0');
   variable_del('user_visits_adv_block_items_1');
-}
\ No newline at end of file
+}
+*/
diff --git a/user_visits_adv.module b/user_visits_adv.module
index 2b017f8..e2b10bd 100644
--- a/user_visits_adv.module
+++ b/user_visits_adv.module
@@ -1,6 +1,8 @@
 <?php
+// $Id: user_visits_adv.module,v 1.6 2009/03/10 15:58:43 toemaz Exp $
 
 /**
+ * @file
  * User visits Advanced
  * Extending user_visits module
  * - store a total number of visits
@@ -13,43 +15,50 @@
  * Implementation of hook_help();
  */
 function user_visits_adv_help($path, $arg) {
+  $output='';
   switch ($path) {
     case "admin/help#user-visits-advaced":
-      $output = '<p>'.  t('Extends the user visits module.') .'</p>';
+      $output = '<p>' . t('Extends the user visits module.') . 	'</p>';
       break;
   }
-
   return $output;
 }
-
 /**
- * Implementation of hook_block().
+ * Implements hook_block_info().
  */
-function user_visits_adv_block($op = 'list', $delta = 0, $edit = array()) {
-  if ($op == 'list') {
-    $blocks[0] = array(
-      'info' => t('My visitors adv'),
-    );
-    $blocks[1] = array(
+function user_visits_adv_block_info() {
+  $blocks['0'] = array(
+    'info' => t('My recent visitors adv'), 
+  );
+  $blocks[1] = array(
       'info' => t('My visitors history adv'),
     );
-    return $blocks;
-  }
-  else if ($op == 'configure') {
-    $form['items'] = array(
+ return $blocks;
+}
+/**
+ * Implements hook_block_configure().
+ */
+function user_visits_adv_block_configure($delta = '0') {
+  $form['items'] = array(
       '#type' => 'select',
       '#title' => t('Number of items'),
-      '#default_value' => variable_get('user_visits_adv_block_items_'. $delta, 5),
+      '#default_value' => variable_get('user_visits_adv_block_items_' . $delta, 5),
       '#options' => drupal_map_assoc(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25)),
     );
     return $form;
-  }
-  else if ($op == 'save') {
-    variable_set('user_visits_adv_block_items_'. $delta, $edit['items']);
-  }
-  else if ($op == 'view') {
-    $limit = variable_get('user_visits_adv_block_items_'. $delta, 5);
-    switch ($delta) {
+}
+/**
+ * Implements hook_block_save().
+ */
+function user_visits_adv_block_save($delta = '0', $edit = array()) {
+  variable_set('user_visits_adv_block_items_' . $delta, $edit['items']);
+}
+/**
+ * Implements hook_block_view().
+ */
+function user_visits_adv_block_view($delta = '') {
+	$limit = variable_get('user_visits_adv_block_items_' . $delta, 5);
+    switch ($delta) { 
       case 0:
         $block = array(
           'subject' => t('Latest visitors'),
@@ -64,9 +73,7 @@ function user_visits_adv_block($op = 'list', $delta = 0, $edit = array()) {
         break;
     }
     return $block;
-  }
 }
-
 /**
  * Get latest visitors for block content
  */
@@ -76,10 +83,10 @@ function user_visits_adv_visitors_block($limit = 5) {
   if ($user->uid) {
     $visitors = user_visits_adv_get('visitors', $user->uid);
     if (is_array($visitors)) {
-      //array_splice($visitors, 0, $limit);
+      $visitors = array_slice($visitors, 0, $limit, TRUE);
       foreach ($visitors as $uid => $visit) {
-        $account = db_fetch_object(db_query('SELECT uid, name, picture FROM {users} WHERE uid = %d', $uid));
-        $output .= theme('user_visits', $account, $visit);
+        $account = db_query('SELECT uid, name, picture FROM {users} WHERE uid = :uid', array(':uid'=> $uid))->fetchObject();
+        $output .= theme('user_visits', array('$account' => $account, '$visit' => $visit));
       }
     }
   }
@@ -91,27 +98,19 @@ function user_visits_adv_visitors_block($limit = 5) {
  */
 function user_visits_adv_history_block($limit = 7) {
   global $user;
-
   if ($user->uid) {
-    $history = user_visits_adv_get('history', $user->uid);
-    $total = user_visits_adv_get('total', $user->uid);
-    if (is_array($history)) {
-      $range = variable_get('user_visits_adv_history_limit', 7);
-
-      $offset = count($history) - $range;
-      switch (true) {
-        case($offset < 0):
-          $history = array_merge(array_fill(0, abs($offset), 0), $history);
-          break;
-        case($offset > 0):
-          $history = array_splice($history, $offset-1, $range, TRUE);
-          break;
-      }
-
-      $output = theme('user_visits_adv_history', $history, $total);
+    // initialize the total number of visits to the users profile page
+    $total = 0;
+    $total += user_visits_adv_get('total', $user->uid);
+    // retrieve the history of the users profile visits and add it up to a clean history array
+    $history_visits = user_visits_adv_get('history', $user->uid);
+    $history = array_fill(user_visits_get_history_key(REQUEST_TIME) - $limit + 1, $limit, 0);
+    foreach ($history as $key => $visits) {
+      $history[$key] += $history_visits[$key];
     }
+//    $output = theme('user_visits_adv_history', array('history' => $history, 'total' => $total));
+    $output=theme_user_visits_adv_history($history,$total);
   }
-
   return $output;
 }
 
@@ -119,7 +118,7 @@ function user_visits_adv_history_block($limit = 7) {
  * Implementation of hook_theme().
  * @return array Definitions of the themable functions
  */
-function user_visits_adv_history_theme() {
+function user_visits_adv_theme() {
   return array(
     'user_visits_adv_history' => array(
       'arguments' => array(
@@ -127,22 +126,18 @@ function user_visits_adv_history_theme() {
         'total' => NULL
       )
     ),
-    'user_visits_adv_get' => array(
-      'arguments' => array(
-        'element' => NULL,
-        'uid' => NULL
-      )
-    ),
   );
 }
 
 /**
  * Theme function for history block
+ * @param $history is array with the views data of the past X days.
+ * X is defined in the configuration of the block
+ * @param $total the total number of views
  */
 function theme_user_visits_adv_history($history, $total) {
-
-  $output = '<div>'. t('!total views in total', array('!total' => $total)) .'</div>';
-  $output .= '<div>'. t('history') .': '. implode(' ', $history) .'</div>';
+  $output = '<div>' . t('!total views in total', array('!total' => $total)) . '</div>';
+  $output .= '<div>' . t('history') .': '. implode(' ', $history) . '</div>';
   return $output;
 }
 
@@ -151,9 +146,8 @@ function theme_user_visits_adv_history($history, $total) {
  */
 function user_visits_adv_get($element, $uid) {
   static $user_visits_adv_data = array();
-
   if (!isset($user_visits_adv_data[$uid])) {
-    if ($data = db_fetch_object(db_query('SELECT total, visitors, history FROM {user_visits_adv} WHERE uid = %d', $uid))) {
+    if ($data = db_query('SELECT total, visitors, history FROM {user_visits_adv} WHERE uid = :uid', array(':uid'=>$uid))->fetchObject()) {
       $user_visits_adv_data[$uid] = array(
         'total' => $data->total,
         'visitors' => unserialize($data->visitors),
@@ -164,45 +158,41 @@ function user_visits_adv_get($element, $uid) {
       $user_visits_adv_data[$uid] = array();
     }
   }
-
   return $user_visits_adv_data[$uid][$element];
 }
-
 /**
- * Implementation of hook_user().
+ * Implements hook_user_delete().
  */
-function user_visits_adv_user($op, &$edit, &$account, $category = NULL) {
-  switch ($op) {
-    case 'delete':
-      db_query('DELETE FROM {user_visits_adv} WHERE uid = %d', $account->uid);
-      break;
-  }
-
+function user_visits_adv_user_delete($account) {
+  $uid= $account->uid;
+  db_query('DELETE FROM {user_visits_adv} WHERE uid = :uid',array(':uid'=>$uid));
+}
+/**
+ * Helper function to create the history key
+ * This key is used in the history array to define the day
+ * @param $time is a UNIX timestamp
+ * @return integer within the interval [0-99356]
+ */
+function user_visits_get_history_key($time) {
+  // constructed with the year [0-99] and the day [0-356]
+  return (date('y', $time) * 1000) + date('z', $time);
 }
 
 /**
  * Implementation of hook_cron().
  */
 function user_visits_adv_cron() {
-
   $visitor_limit = variable_get('user_visits_adv_visitor_limit', 20);
-
-  // make a referencial timestamp used to remove the collected records
-  $timestamp = time();
-
-  $visits = db_query('SELECT uid, COUNT(*) as count FROM {user_visits} GROUP BY uid');
-  while ($visit = db_fetch_object($visits)) {
-
-    $visitors = db_query('SELECT vuid, visit FROM {user_visits} WHERE uid = %d ORDER BY visit DESC', $visit->uid);
-
+  $visits = db_query('SELECT uid FROM {user_visits} GROUP BY uid')->fetchObject();
+  foreach ($visits as $visit) {
+    $visitors = db_query('SELECT vuid, visit FROM {user_visits} WHERE uid = :uid ORDER BY visit DESC', array(':uid'=> $visit->uid))->fetchObject();
     $visit->visitors = array();
     $visit->recent_visitors = array();
     $visit->count = 0;
-
-    while ($visitor = db_fetch_object($visitors)) {
+    foreach ($visitors as $visitor) {
       // creating unique indentifier for the day
-      $date = (date('y', $visitor->visit) * 1000) + date('z', $visitor->visit);
-      $visit->visitors[$date]++;
+      $key = user_visits_get_history_key($visitor->visit);
+      $visit->visitors[$key]++;
       if ($visit->count < $visitor_limit) {
         $visit->recent_visitors[$visitor->vuid] = $visitor->visit;
       }
@@ -210,8 +200,7 @@ function user_visits_adv_cron() {
     }
 
     //retrieve the history array
-    if ($data = db_fetch_object(db_query('SELECT history, visitors FROM {user_visits_adv} WHERE uid = %d', $visit->uid))) {
-
+    if ($data = db_query('SELECT history, visitors FROM {user_visits_adv} WHERE uid = :uid',array(':uid'=>$visit->uid))->fetchObject()){
       $history = unserialize($data->history);
       // add daily visits to the history array
       foreach ($visit->visitors as $key => $count) {
@@ -231,13 +220,18 @@ function user_visits_adv_cron() {
         $visit->recent_visitors += $visitors;
         arsort($visit->recent_visitors, SORT_NUMERIC);
       }
-
       // store all data
-      db_query("UPDATE {user_visits_adv} SET total = total + %d, visitors = '%s', history = '%s' WHERE uid = %d", $visit->count, serialize($visit->recent_visitors), serialize($history), $visit->uid);
+      db_update('user_visit_adv')
+        ->fields(array(
+        'total' => total + $visit->count,
+        'visitors' => serialize($visit->recent_visitors),
+        'history' => serialize($history), $visit->uid,
+        ))
+        ->conditions('uid',$visit->count)
+        ->execute();
     }
     else {
       $range = variable_get('user_visits_adv_history_limit', 7);
-
       $history = array();
       foreach ($visit->visitors as $key => $count) {
         $history[$key] = $count;
@@ -247,22 +241,26 @@ function user_visits_adv_cron() {
       if (count($history) > $range) {
         $history = array_slice($history, count($history) - $range, $range, TRUE);
       }
-
-      db_query("INSERT INTO {user_visits_adv} (uid, total, visitors, history) VALUES (%d, %d, '%s', '%s')", $visit->uid, $visit->count, serialize($visit->recent_visitors), serialize($history));
+      db_insert('user_visit_adv')
+      ->fields(array(
+      'uid' => $visit->uid,
+      'total' => $visit->count,
+      'visitors' => serialize($visit->recent_visitors),
+      'history' => serialize($history),
+      ))
+      ->execute();
     }
   }
-
-  // remove old data which has just been collected
-  //db_query('DELETE FROM {user_visits} WHERE time < %d', $timestamp);
-
+  // all data is harvested, so empty the user_visits table
+  db_query('DELETE FROM {user_visits}');
 }
 
 /**
  * Implementing hook_form_alter
  */
-function user_visits_adv_form_alter(&$form, $form_state, $form_id) {
+function user_visits_adv_form_alter(&$form, &$form_state, $form_id) {
   if ($form_id == 'user_visits_settings') {
-    $form['user_visits']['user_visits_advanced'] = array(
+    $form['user_visits	']['user_visits_advanced'] = array(
       '#type' => 'fieldset',
       '#title' => t('Advanced'),
       '#weight' => 1,
@@ -284,4 +282,4 @@ function user_visits_adv_form_alter(&$form, $form_state, $form_id) {
       '#description' => t('Maximum number of visitors to collect per user.'),
     );
   }
-}
\ No newline at end of file
+}
