diff --git a/css/time_tracker.css b/css/time_tracker.css
index d0ee302..c8ed336 100644
--- a/css/time_tracker.css
+++ b/css/time_tracker.css
@@ -27,28 +27,30 @@
   padding: 0px;
 }
 
-#edit-username-wrapper,
+
+.form-item-username,
 #edit-time-wrapper,
-#edit-duration-wrapper,
-#edit-activity-wrapper,
+
+.form-item-duration,
+.form-item-activity,
 #edit-deductions-wrapper {
   float:left;
 }
 
-#edit-duration-wrapper input {
+.form-item-duration input {
   width: 60px;
 }
 
-#edit-username-wrapper {
+.form-item-username {
   /*margin-right: 20px;*/
 /*  width: 200px;*/
 }
 
-#edit-username-wrapper input {
+.form-item-username input {
   /*width: 200px;*/
 }
 
-#edit-activity-wrapper select {
+.form-item-activity select {
 /*  min-width: 200px;*/
 }
 
@@ -109,7 +111,7 @@
 #edit-deductions-1-wrapper {
   float:left;
 }
-
+#comment-form .form-item,
 #edit-locked-1-wrapper,
 #edit-billable-1-wrapper,
 #edit-billed-1-wrapper {
diff --git a/time_tracker.info b/time_tracker.info
index 016426b..bad65fd 100644
--- a/time_tracker.info
+++ b/time_tracker.info
@@ -1,8 +1,7 @@
-; $Id: time_tracker.info,v 1.6 2010/05/28 16:30:29 codi Exp $
 name = Time Tracker
 description = Provides time tracking functionality nodes and comments.
 package = Time Tracker
-core = 6.x
+core = 7.x
 dependencies[] = views
 dependencies[] = views_calc
 dependencies[] = date_popup
\ No newline at end of file
diff --git a/time_tracker.install b/time_tracker.install
index 63b0f6a..6a34feb 100644
--- a/time_tracker.install
+++ b/time_tracker.install
@@ -1,9 +1,7 @@
 <?php
-// $Id: time_tracker.install,v 1.8 2010/06/11 21:48:22 codi Exp $
-
 /**
  * @file
- * Implementation of hook_install().
+ * Implements hook_schema().
  */
 function time_tracker_schema() {
   $schema = array();
@@ -108,152 +106,18 @@ function time_tracker_schema() {
   return $schema;
 }
 
-function time_tracker_update_6100() {
-  $return = array();
-  db_add_field($return, 'time_tracker_entry', 'pid', array('type' => 'int', 'not null' => TRUE, 'unsigned' => TRUE));
-  return $return;
-}
-
-/**
- * Adding support for start and end times rather than just durations
- */
-function time_tracker_update_6101() {
-  $return = array();
-  db_add_field($return, 'time_tracker_entry', 'start', array('type' => 'int', 'not null' => TRUE, 'unsigned' => TRUE));
-  db_add_field($return, 'time_tracker_entry', 'end', array('type' => 'int', 'not null' => TRUE, 'unsigned' => TRUE));
-  return $return;
-}
-
-/**
- * Weighs the time_tracker module so it comes before most other modules.
- */
-function time_tracker_update_6102() {
-  $return = array();
-  $return[] = $ret[] = update_sql("UPDATE {system} SET weight = -1 WHERE name = 'time_tracker'");
-  return $return;
-}
-
-/**
- * Weighs the time_tracker module so it comes before most other modules.
- */
-function time_tracker_update_6103() {
-  $return = array();
-  db_add_field($return, 'time_tracker_entry', 'note', array('type' => 'text', 'not null' => TRUE));
-  return $return;
-}
-
-/**
- * Adds locked field to time_tracker_entry
- */
-function time_tracker_update_6104() {
-  $return = array();
-  db_add_field($return, 'time_tracker_entry', 'locked', array('type' => 'int', 'size' => 'tiny'));
-  return $return;
-}
-
-/**
- * Adds status field to time_tracker_entry
- */
-function time_tracker_update_6105() {
-  $return = array();
-  db_add_field($return, 'time_tracker_activity', 'status', array('type' => 'int', 'size' => 'tiny'));
-  db_query("UPDATE {time_tracker_activity} SET status = 1 WHERE 1");
-  return $return;
-}
-
-/**
- * Adds deductions and billed fields to time_tracker_entry
- */
-function time_tracker_update_6106() {
-  $return = array();
-  db_add_field($return, 'time_tracker_entry', 'deductions', array('type' => 'float', 'not null' => TRUE, 'unsigned' => TRUE));
-  db_add_field($return, 'time_tracker_entry', 'billed', array('type' => 'int', 'size' => 'tiny'));
-  return $return;
-}
-
 /**
- * Adds deductions and billed fields to time_tracker_entry
- */
-function time_tracker_update_6107() {
-  $return = array();
-  db_drop_field($return, 'time_tracker_entry', 'pid');
-  return $return;
-}
-
-/**
- * Adds default values to columns
- */
-function time_tracker_update_6108() {
-  $return = array();
-  db_change_field($return, 'time_tracker_entry', 'cid', 'cid', array(
-    'type' => 'int',
-    'not null' => TRUE,
-    'unsigned' => TRUE,
-    'default' => 0,
-  ));
-  db_change_field($return, 'time_tracker_entry', 'uid', 'uid', array(
-    'type' => 'int',
-    'not null' => TRUE,
-    'unsigned' => TRUE,
-    'default' => 0,
-  ));
-  db_change_field($return, 'time_tracker_entry', 'activity', 'activity', array(
-    'type' => 'int',
-    'unsigned' => TRUE,
-    'default' => 0,
-  ));
-  db_change_field($return, 'time_tracker_entry', 'timestamp', 'timestamp', array(
-    'type' => 'int',
-    'not null' => TRUE,
-    'default' => 0,
-  ));
-  db_change_field($return, 'time_tracker_entry', 'start', 'start', array(
-    'type' => 'int',
-    'unsigned' => TRUE,
-    'default' => 0,
-  ));
-  db_change_field($return, 'time_tracker_entry', 'end', 'end', array(
-    'type' => 'int',
-    'unsigned' => TRUE,
-    'default' => 0,
-  ));
-  db_change_field($return, 'time_tracker_entry', 'deductions', 'deductions', array(
-    'type' => 'float',
-    'unsigned' => TRUE,
-    'default' => 0,
-  ));
-  db_change_field($return, 'time_tracker_entry', 'duration', 'duration', array(
-    'type' => 'float',
-    'not null' => TRUE,
-    'unsigned' => FALSE,
-    'default' => 0,
-  ));
-  db_change_field($return, 'time_tracker_entry', 'note', 'note', array(
-    'type' => 'text',
-    'default' => '',
-  ));
-  return $return;
-}
-
-/**
- * Adds default values to columns
+ * @file
+ * Implements hook_install().
  */
-function time_tracker_update_6109() {
-  $return = array();
-  // Type text can't have a default
-  db_change_field($return, 'time_tracker_entry', 'note', 'note', array(
-    'type' => 'text',
-  ));
-  return $return;
-}
-
-
 function time_tracker_install() {
-  drupal_install_schema('time_tracker');
-  db_query("UPDATE {system} SET weight = %d WHERE name = '%s'", -1, 'time_tracker');
-  drupal_set_message(t("Time Tracker has been installed successfully. To begin tracking time, you first must enable time tracking on at least one content type. You can do so on any content type's edit page at admin/content/node-type/%content-type-name"));
-}
+  
+  db_update('system')
+    ->fields(array(
+      'weight' => -1,
+    )) 
+    ->condition('name', 'time_tracker', '=')
+    ->execute();
 
-function time_tracker_uninstall() {
-  drupal_uninstall_schema('time_tracker');
+  drupal_set_message(t("Time Tracker has been installed successfully. To begin tracking time, you first must enable time tracking on at least one content type. You can do so on any content type's edit page at admin/content/node-type/%content-type-name"));
 }
diff --git a/time_tracker.module b/time_tracker.module
index 3080e49..dab365d 100644
--- a/time_tracker.module
+++ b/time_tracker.module
@@ -1,6 +1,4 @@
 <?php
-// $Id: time_tracker.module,v 1.31 2010/07/22 00:32:32 codi Exp $
-
 /**
  * @file
  * Enables time tracking on nodes and comments.
@@ -27,18 +25,46 @@ define('TIME_TRACKER_COMMENT', 'comment');
 /**
  * Implementation of hook_perm().
  */
-function time_tracker_perm() {
-  return array('administer time tracker', 'add time tracker entries',
-    'view all time tracker entries', 'view own time tracker entries', 'edit time tracker entries',
-    'delete time tracker entries', 'administer time entries');
+function time_tracker_permission() {
+  return array(
+    'administer time tracker' => array(
+      'title' => t('Administer Time Tracker'),
+      'description' => t('TODO: Add descriptions'),
+    ),
+    'add time tracker entries' => array(
+      'title' => t('Add Time Tracker Entries'),
+      'description' => t('TODO: Add descriptions'),
+    ),
+    'view all time tracker entries' => array(
+      'title' => t('View All Time Tracker Entries'),
+      'description' => t('TODO: Add descriptions'),
+    ),
+    'view own time tracker entries' => array(
+      'title' => t('View Own Time Tracker Entries'),
+      'description' => t('TODO: Add descriptions'),
+    ),
+    'edit time tracker entries' => array(
+      'title' => t('Edit Time Tracker Entries'),
+      'description' => t('TODO: Add descriptions'),
+    ),
+    'delete time tracker entries' => array(
+      'title' => t('Delete Time Tracker Entries'),
+      'description' => t('TODO: Add descriptions'),
+    ), 
+    'administer time entries'=> array(
+      'title' => t('Administer Time Tracker Entries'),
+      'description' => t('TODO: Add descriptions'),
+    ),
+  );
 }
 
 /**
- * Implementation of hook_help().
+ * Implements hook_help().
  */
-function time_tracker_help($section, $arg) {
-  switch ($section) {
+function time_tracker_help($path, $arg) {
+  switch ($path) {
     case 'admin/help#time_tracker':
+      // TODO: Remove html from t().
       return t("<h2>Time Tracker</h2>
         <p>To begin tracking time go to the edit content type screen for any content type (admin/content/node-type/%content-type-name) and scroll to the 'Time Tracker Settings' fieldset. There, you can choose to track time either on this content type's nodes or this content type's comments. Tracking time on nodes will give you a time entry sheet and a table of time entries at the bottom of a node's content, but before the comments. Tracking time on comments will add a time entry sheet to the comment form. Keep in mind that if you choose to track time on a content type's nodes as well as comments, you will only ever see comment time entries in the comment thread, and node time entries in the time entry table. However, if you were to use views to view the time entries, you could see them all with no distinction.</p>
         <p>We recommend deciding ahead of time whether or not you would like to track time on comments or nodes. We've found that people who just want to jot down time entries with minimal notes prefer to track time on nodes. However
@@ -84,25 +110,25 @@ function time_tracker_help($section, $arg) {
 }
 
 /**
- * Implementation of hook_menu().
+ * Implements hook_menu().
  *
  */
 function time_tracker_menu() {
   $items = array();
 
-  $items['admin/settings/time_tracker'] = array(
+  $items['admin/config/people/time_tracker'] = array(
     'title' => 'Time Tracker',
     'description' => 'Adjust time tracker settings.',
     'page callback' => 'drupal_get_form',
     'page arguments' => array('time_tracker_settings_form'),
     'access arguments' => array('administer time tracker'),
   );
-  $items['admin/settings/time_tracker/settings'] = array(
+  $items['admin/config/people/time_tracker/settings'] = array(
     'title' => 'Time Tracker',
     'type' => MENU_DEFAULT_LOCAL_TASK,
     'weight' => -10,
   );
-  $items['admin/settings/time_tracker/activity/list'] = array(
+  $items['admin/config/people/time_tracker/activity/list'] = array(
     'title' => 'Activities',
     'description' => 'Adjust time tracker settings.',
     'page callback' => 'drupal_get_form',
@@ -111,10 +137,10 @@ function time_tracker_menu() {
     'type' => MENU_LOCAL_TASK,
     'parent' => 'admin/settings/time_tracker',
   );
-  $items['admin/settings/time_tracker/activity/delete/%'] = array(
+  $items['admin/config/people/time_tracker/activity/delete/%'] = array(
     'title' => 'Delete Activity',
     'page callback' => 'drupal_get_form',
-    'page arguments' => array('time_tracker_delete_activity_confirm', 5),
+    'page arguments' => array('time_tracker_delete_activity_confirm', 6),
     'access arguments' => array('administer time tracker'),
   );
   $items['time_entry/edit/%'] = array(
@@ -130,20 +156,19 @@ function time_tracker_menu() {
 
 /**
  * What's this all about??
- */
-function time_tracker_user_page($user) {
-  $output = views_embed_view('time_tracker_user', 'default', $user->uid);
+ *
+ *function time_tracker_user_page($user) {
+ *  $output = views_embed_view('time_tracker_user', 'default', $user->uid);
+ *
+ *  return $output;
+ *}
+ **/
 
-  return $output;
-}
 
 /**
  * Form builder function for time tracker settings.
  */
-function time_tracker_settings_form(&$form_state) {
-
-  // Warn the user about switching between time duration and time interval time entries
-  $msg = t('Note that any entries that have been entered using the <em>duration</em> method will not have start and end times saved in the database. Switching from <em>duration</em> to <em>interval</em> will not retroactively create start and end times for past time entires.');
+function time_tracker_settings_form($form, &$form_state) {
 
   $form = array();
 
@@ -179,7 +204,7 @@ function time_tracker_settings_form(&$form_state) {
   $form['time_tracker_settings']['enable_deductions_field'] = array(
     '#type' => 'checkbox',
     '#title' => t("Enable the 'Deductions' field"),
-    '#description' => t("Checking this setting will enable a text field for logging time entry deductions. An example usage of this field would be for a time entriy from 9:00am to 5:00pm with a deduction of 30 minutes for a lunch break."),
+    '#description' => t("Checking this setting will enable a text field for logging time entry deductions. An example usage of this field would be for a time entry from 9:00am to 5:00pm with a deduction of 30 minutes for a lunch break."),
     '#default_value' => variable_get('enable_deductions_field', 0),
   );
 
@@ -231,19 +256,22 @@ function time_tracker_settings_form(&$form_state) {
       'interval' => t('Time Interval (Start and End Times)')
     ),
   );
+
+  // Warn the user about switching between time duration and time interval time entries
+  $msg = t('Note that any entries that have been entered using the <em>duration</em> method will not have start and end times saved in the database. Switching from <em>duration</em> to <em>interval</em> will not retroactively create start and end times for past time entires.');
   $form['time_entry_settings']['time_entry_message'] = array(
     '#type' => 'markup',
     '#prefix' => '<div class="description">',
     '#suffix' => '</div>',
     '#value' => $msg,
   );
-
-  // Date formats
-  $msg = 'Dates will be output using format_date(). Below, please specify PHP date format strings as required by <a href="http://php.net/manual/en/function.date.php">date()</a>. A backslash should be used before a character to avoid interpreting the character as part of a date format.';
   $form['time_tracker_date_formats'] = array(
     '#type' => 'fieldset',
     '#title' => t('Time Tracker Date Formats'),
   );
+
+  // Date formats
+  $msg = 'Dates will be output using format_date(). Below, please specify PHP date format strings as required by <a href="http://php.net/manual/en/function.date.php">date()</a>. A backslash should be used before a character to avoid interpreting the character as part of a date format.';
   $form['time_tracker_date_formats']['time_entry_message'] = array(
     '#type' => 'markup',
     '#prefix' => '<div class="description">',
@@ -302,7 +330,6 @@ function time_tracker_settings_form(&$form_state) {
   );
 
   return system_settings_form($form);
-
 }
 
 
@@ -311,7 +338,7 @@ function time_tracker_settings_form(&$form_state) {
  */
 function time_tracker_views_api() {
   return array(
-    'api' => 2,
+    'api' => 3,
     'path' => drupal_get_path('module', 'time_tracker') .'/views',
   );
 }
@@ -334,31 +361,44 @@ function theme_time_tracker_activity_table($form) {
     t('Delete'),
     t('Weight')
   );
+  // For some reason form comes in an empty array...
+  $form = $form[''];
+
   // Take all the form elements and format theme for theme_table
   foreach (element_children($form['activities']) as $key) {
     if (isset($form['activities'][$key]['name'])) {
+      // Add the table drag functionality
+      drupal_add_tabledrag('activity-table', 'order', 'sibling', 'activity-weight');
       // Add class to group weight fields for drag and drop.
-      $form['activities'][$key]['weight']['#attributes']['class'] = 'activity-weight';
+      $form['activities'][$key]['weight']['#attributes']['class'] = array('activity-weight');
       // array to store row data
-      $row = array();
-      $row[] = drupal_render($form['activities'][$key]['name']);
-      $row[] = drupal_render($form['activities'][$key]['status']);
-      $row[] = drupal_render($form['activities'][$key]['delete']);
-      $row[] = drupal_render($form['activities'][$key]['weight']);
-      $rows[] = array('data' => $row, 'class' => 'draggable');
+
+      $rows[] = array(
+	'data' => array(
+          drupal_render($form['activities'][$key]['name']),
+          drupal_render($form['activities'][$key]['status']),
+          drupal_render($form['activities'][$key]['delete']),
+          drupal_render($form['activities'][$key]['weight']),
+        ),
+        'class' => array('draggable'),
+      ); 
     }
   }
+  $table = array(
+  	'header' => $header,
+        'rows' => $rows,
+        'attributes' => array('id' => array('activity-table')),
+  );
+
   // Theme it as a table
-  $output = theme('table', $header, $rows, array('id' => 'activity-table'));
-  $output .= drupal_render($form['add_new_activity']);
-  // Render the form
-  $output .= drupal_render($form);
-  // Add the table drag functionality
-  drupal_add_tabledrag('activity-table', 'order', 'sibling', 'activity-weight');
+  $output = theme('table', $table);
+
+  // Render the form 
+  $output .= drupal_render_children($form);
+
   // Return the themed activities table
   return $output;
 }
-
 /**
  * Time tracker activities table form.
  *
@@ -371,9 +411,14 @@ function theme_time_tracker_activity_table($form) {
 function time_tracker_activity_table_form() {
 
   // Grab all the activities ordered by weight
-  $results = db_query('SELECT * FROM {time_tracker_activity} ORDER BY weight ASC');
+ 
+  $results = db_select('time_tracker_activity', 'tta')
+    ->fields('tta')
+    ->orderBy('weight', 'ASC')
+    ->execute()->fetchAllAssoc('taid');
   // Put activities in an array
-  while ($result = db_fetch_object($results)) {
+
+  foreach ($results as $result) {
     $activities[$result->taid] = $result;
   }
 
@@ -452,14 +497,30 @@ function time_tracker_activity_table_form_submit($form, &$form_state) {
     $activity['name'] = $form_state['values']['add_new_activity']['new_activity_name'];
     $activity['weight'] = count($form_state['values']['activities']) + 1;
     $activity['status'] = 1;
-    drupal_write_record('time_tracker_activity', $activity);
+    db_insert('time_tracker_activity')
+      ->fields(array(
+        'name' => $form_state['values']['add_new_activity']['new_activity_name'],
+        'weight' => count($form_state['values']['activities']) + 1,
+        'status' => 1,
+      ))
+    ->execute();
   }
   if (count($form_state['values']['activities']) > 0) {
     foreach ($form_state['values']['activities'] as $taid => $activity) {
+
       // If anything has changed...
-      if (is_numeric($taid) && ($form[$taid]['#activity']['weight'] != $activity['weight'] || $form[$taid]['#activity']['status'] != $activity['status'] || $form[$taid]['#activity']['name'] != $activity['name'])) {
-        $activity['taid'] = $taid;
-        drupal_write_record('time_tracker_activity', $activity, array('taid'));
+      if (is_numeric($taid) && ($form['activities'][$taid]['#activity']['weight'] != $activity['weight'] || 
+                                $form['activities'][$taid]['#activity']['status'] != $activity['status'] || 
+                                $form['activities'][$taid]['#activity']['name'] != $activity['name'])) {
+         
+        db_update('time_tracker_activity')
+          ->fields(array(
+            'name' => $activity['name'],
+            'weight' => $activity['weight'],
+            'status' => $activity['status'],
+          ))
+          ->condition('taid', $taid, '=')
+          ->execute();
       }
     }
   }
@@ -469,7 +530,7 @@ function time_tracker_activity_table_form_submit($form, &$form_state) {
 /**
  * Form builder function for module time tracker activities.
  */
-function time_tracker_delete_activity_confirm(&$form_state, $taid) {
+function time_tracker_delete_activity_confirm($form, &$form_state, $taid) {
 
   $form = array();
 
@@ -478,7 +539,7 @@ function time_tracker_delete_activity_confirm(&$form_state, $taid) {
       '#type' => 'value',
       '#default_value' => $taid,
     );
-    $question = t('Are you sure you want to delete the activity: !activity_name', array('!activity_name' => get_activity_name($taid)));
+    $question = t('Are you sure you want to delete the activity: !activity_name', array('!activity_name' => _time_tracker_get_activity_name($taid)));
     return confirm_form($form, $question, 'admin/settings/time_tracker/activity/list');
   }
   else {
@@ -494,7 +555,9 @@ function time_tracker_delete_activity_confirm(&$form_state, $taid) {
  */
 function time_tracker_delete_activity_confirm_submit($form, &$form_state) {
   $form_state['redirect'] = 'admin/settings/time_tracker/activity/list';
-  db_query('DELETE FROM {time_tracker_activity} WHERE taid = ' . $form_state['values']['taid']);
+  db_delete('time_tracker_activity')
+    ->condition('taid', $form_state['values']['taid'], '=')
+    ->execute();
   drupal_set_message(t('Activity %name Deleted', array('%name' => $form_state['values']['name'])));
 }
 
@@ -507,14 +570,15 @@ function time_tracker_delete_activity_confirm_submit($form, &$form_state) {
  *
  * Adds the options for tracking time to the
  */
-function time_tracker_form_alter(&$form, $form_state, $form_id) {
+function time_tracker_form_node_type_form_alter(&$form, &$form_state, $form_id) {
   // what are we tracking time on?
-  if ($form_id == 'node_type_form' && isset($form['identity']['type'])) {
+  if (isset($form['type'])) {
     $form['time_tracker'] = array(
       '#type' => 'fieldset',
       '#title' => t('Time Tracker settings'),
       '#collapsible' => TRUE,
       '#collapsed' => TRUE,
+      '#group' => 'additional_settings',
     );
     $form['time_tracker']['time_tracker_nodes'] = array(
       '#type' => 'checkbox',
@@ -529,175 +593,207 @@ function time_tracker_form_alter(&$form, $form_state, $form_id) {
       '#description' => t('Track time on this content types comments.'),
     );
   }
-
+}
+function time_tracker_form_comment_form_alter(&$form, &$form_state, $form_id) {
   // if this is a comment form and we're tracking time on it.
-  if ($form_id == 'comment_form' && user_access('add time tracker entries')) {
-
+  if (user_access('add time tracker entries')) {
     $node = node_load($form['nid']['#value']);
-
     if (variable_get('time_tracker_comments_' . $node->type, 0)) {
-      $form = array_merge($form, time_tracker_time_entry_form(NULL, 'comment', $form['cid']['#value']));
+      $form['time_tracker'] = array(
+        '#type' => 'fieldset',
+        '#title' => t('Time Tracker'),
+        '#collapsible' => TRUE,
+        '#collapsed' => variable_get('time_tracker_default_collapsed', 0),
+  //  '#weight' => -9,
+  );
+      $form['time_tracker']['entry_form'] = drupal_get_form('time_tracker_time_entry_form', 'comment', $form['cid']['#value']);
     }
   }
-
 }
 
 /**
- * Implementation of hook_nodeapi().
+ * Implementation of hook_node_view().
  */
-function time_tracker_nodeapi(&$node, $op) {
-  switch ($op) {
-    case 'view':
-      if (user_access('view all time tracker entries') || user_access('view own time tracker entries')) {
-        // init total time
-        $total_time = 0;
-        // If case tracker exists then we can check if it's a project
-        if (module_exists('casetracker') && casetracker_is_project($node->type)) {
-          $case_results = db_query("SELECT * FROM {casetracker_case} WHERE pid = %d", $node->nid);
-
-          while ($case = db_fetch_object($case_results)) {
-            $time_results = db_query("SELECT * FROM {time_tracker_entry} WHERE nid = %d", $case->nid);
-
-            while ($time = db_fetch_object($time_results)) {
-              $total_time = $total_time + ($time->duration - $time->deductions);
-            }
-          }
-          if ($total_time > 0) {
-            $node->content['time_tracker_summary'] = array(
-              '#value' => theme('time_tracker_summary', $total_time),
-              '#weight' => -5,
-            );
-          }
-
-        }
-        else {
-
-          $total_time = time_tracker_get_total_logged_time($node->nid);
+function time_tracker_node_view($node, $view_mode, $langcode) {
+	
+  if ($view_mode == 'full') {
+    if (user_access('view time tracker entries')) {
+      // if this is a node form display the time tracker
+      $resource = db_query("SELECT * FROM {time_tracker_entry} WHERE nid = :nid", array(':nid' => $node->nid));
+      $total_time = 0;
+      while ($time_entry = $resource->fetchObject()) {
+        $total_time = $total_time + $time_entry->duration;
+      }
 
-          if ($total_time > 0) {
-            $node->content['time_tracker_summary'] = array(
-              '#value' => theme('time_tracker_summary', $total_time),
-              '#weight' => -5,
-            );
-          }
-        }
+      $node->content['time_tracker'] = array(
+      	'#type' => 'fieldset',
+        '#title' => t('Time Tracker'),
+        '#collapsible' => TRUE,
+        '#collapsed' => variable_get('time_tracker_default_collapsed', 0),
+        '#weight' => 50,
+      );
 
-        // display the time tracking form for nodes here.
-        if (variable_get('time_tracker_nodes_' . $node->type, 0)) {
-          // Store time entries from nodes
-          $node_time_entries = array();
-          $sql = "SELECT * FROM {time_tracker_entry} AS t
-                  WHERE nid = %d AND cid = 0
-                  ORDER BY t.timestamp DESC, t.start DESC, t.teid DESC";
-          // Get the entries associated with this node
-          $resource = db_query($sql, $node->nid);
-          // Store the db objects in an array for theme function
-          while ($time_entry = db_fetch_object($resource)) {
-            $node_time_entries[] = $time_entry;
-          }
-          $node->content['time_tracker'] = array(
-            '#value' => theme('time_tracker_time_entry_table', $node_time_entries) .
-                        (user_access('add time tracker entries') ? drupal_get_form('time_tracker_time_entry_form', 'node', $node->nid) : ''),
-            '#weight' => 51,
-          );
+      if ($total_time > 0) {
+	
+        $node->content['time_tracker']['time_tracker_summary'] = array(
+          '#markup' => theme('time_tracker_summary', $total_time),
+          //'#weight' => -5,
+        );
+      }
+      
+
+      // display the time tracking form for nodes here.
+      if (variable_get('time_tracker_nodes_' . $node->type, 0)) {
+        // Store time entries from nodes
+        $node_time_entries = array();
+        $sql = "SELECT * FROM {time_tracker_entry} AS t
+                WHERE nid = :nid AND cid = 0
+                ORDER BY t.timestamp DESC, t.start DESC, t.teid DESC";
+        // Get the entries associated with this node
+        $resource = db_query($sql, array(':nid' => $node->nid));
+        // Store the db objects in an array for theme function
+        while ($time_entry = $resource->fetchObject()) {
+          $node_time_entries[] = $time_entry;
         }
+        $node->content['time_tracker']['entries'] = array(
+          '#markup' => theme('time_tracker_time_entry_table', $node_time_entries),
+         // '#weight' => 50,
+        );
+        $node->content['time_tracker']['entry_form'] = drupal_get_form('time_tracker_time_entry_form', 'node', $node->nid);
       }
-    break;
-
-    case 'delete':
-      db_query("DELETE FROM {time_tracker_entry} WHERE nid = %d", $node->nid);
-      db_query("DELETE FROM {time_tracker_timer} WHERE nid = %d", $node->nid);
-    break;
-
-    case 'insert':
-    case 'update':
-
-    break;
+    }
   }
 }
 
 /**
- * Implementation of hook_comment().
- *
- * TODO: previews aren't showing any time_tracker data.
+ * Implementation of hook_node_delete().
  */
-function time_tracker_comment(&$comment, $op) {
-
-  switch ($op) {
-    case 'view':
-    // We only want to show time tracker data if user has permission
-      if (user_access('view all time tracker entries') || (user_access('view own time tracker entries') && $GLOBALS['user']->uid == $comment->uid)) {
-        // If this is a preview we won't have a cid yet.
-        if (empty($comment->cid)) {
-          $time_tracker_data = (object)$comment->time_tracker;
-          $node = node_load($comment->nid);
-        }
-        else {
-          $results = db_query("SELECT * FROM {time_tracker_entry} WHERE cid = %d", $comment->cid);
-          while ($result = db_fetch_object($results)) {
-            $time_tracker_data = $result;
-          }
-        }
-        if (is_object($time_tracker_data)) {
-          // This will flag the comment so it can be hidden
-          // hide flag is triggered in the preprocess function below
-          $comment->status = 2;
-        }
-        $comment->comment = theme('time_tracker_comment', $time_tracker_data) . $comment->comment;
-      }
-    break;
-
-    case 'validate':
-      $form_state['values'] = $comment;
-      time_tracker_time_entry_validate(NULL, $form_state);
+function time_tracker_node_delete($node) {
+  db_delete('time_tracker_entry')
+    ->condition('nid', $node->nid)
+    ->execute();
+  db_delete('time_tracker_timer')
+    ->condition('nid', $node->nid)
+    ->execute();
+}
 
-    break;
+/**
+ * Implementation of hook_comment_view().
+ */
+function time_tracker_comment_view($comment, $view_mode, $langcode) {
 
-    case 'insert':
-    case 'update':
-      $rounding_interval = variable_get('time_tracker_rounding_interval', 0);
-      $rounding_operation = variable_get('time_tracker_rounding_operation', 'auto');
-      // Round the deductions if necessary
-      $comment['deductions'] = isset($comment['deductions']) ? time_tracker_round(_time_tracker_parse_duration($comment['deductions']), $rounding_interval / 60, $rounding_operation) : NULL;
-      // Check First if we are tracking by duration or time interval
-      if (variable_get('time_entry_method', 'duration') == 'duration') {
-        $duration = time_tracker_round(_time_tracker_parse_duration($comment['duration']), $rounding_interval / 60, $rounding_operation);
-        if ($duration > 0) {
-          $comment['duration'] = $duration;
-          $comment['timestamp'] = strtotime($comment['time']);
-          unset($comment['time']);
-          // Update or create the time entry
-          return drupal_write_record('time_tracker_entry', $comment, $comment['teid'] ? 'teid' : array());
-        }
-        elseif ($comment['teid']) {
-          // No duration, but there WAS duration information -- someone wants
-          // to remove the time entry!
-          db_query("DELETE FROM {time_tracker_entry} WHERE teid = %d", $comment['teid']);
-        }
-      }
-      elseif (variable_get('time_entry_method', 'duration') == 'interval') {
-        if (strlen($comment['start']) && strlen($comment['end'])) {
-          $comment['start'] = $comment['timestamp'] = time_tracker_round(strtotime($comment['start']), $rounding_interval * 60, $rounding_operation);
-          $comment['end'] = time_tracker_round(strtotime($comment['end']), $rounding_interval * 60, $rounding_operation);
-          //Do we make the duration into a timestamp now??
-          $comment['duration'] = _time_tracker_parse_duration(convert_phptime_to_duration( $comment['start'], $comment['end']));
-          return drupal_write_record('time_tracker_entry', $comment, $comment['teid'] ? 'teid' : array());
-        }
-        elseif ($comment['teid']) {
-          // No duration, but there WAS duration information -- someone wants
-          // to remove the time entry!
-          db_query("DELETE FROM {time_tracker_entry} WHERE teid = %d", $comment['teid']);
-        }
+  // We only want to show time tracker data if user has permission
+  global $user;
+  if (user_access('view all time tracker entries') || (user_access('view own time tracker entries') && $user->uid == $comment->uid)) {
+    // If this is a preview we won't have a cid yet.
+    if (empty($comment->cid)) {
+      $time_tracker_data = (object)$comment->time_tracker;
+      $node = node_load($comment->nid);
+    }
+    else {
+      $results = db_query("SELECT * FROM {time_tracker_entry} WHERE cid = :cid", array(':cid' => $comment->cid));
+      while ($result = $results->fetchObject()) {
+        $time_tracker_data = $result;
       }
+    }
+    if (is_object($time_tracker_data)) {
+      // This will flag the comment so it can be hidden
+      // hide flag is triggered in the preprocess function below
+      $comment->status = 2;
+    }
+    $comment->content['comment_body'] = theme('time_tracker_comment', $time_tracker_data) . $comment->comment;
+
+  }
+}
 
-    break;
+/**
+ * Implements hook_comment_insert().
+ * Because we want to do the same thing for both insert and update
+ * we asbtract the process into its own function and call it with both
+ * insert and update hooks.
+ */
+function time_tracker_comment_insert($comment){	
+  //time_tracker_comment_insert_update($comment);
+}
 
-    case 'delete':
-      db_query("DELETE FROM {time_tracker_entry} WHERE cid = %d", $comment->cid);
-    break;
+/**
+ * Implements hook_comment_update().
+ */
+function time_tracker_comment_update($comment){
+  time_tracker_comment_insert_update($comment);
+}
+
+/**
+ * Process for time_tracker_comment_update() and time_tracker_comment_insert().
+ */
+function time_tracker_comment_insert_update($comment) {
+	
+  $rounding_interval = variable_get('time_tracker_rounding_interval', 0);
+  $rounding_operation = variable_get('time_tracker_rounding_operation', 'auto');
+  // Round the deductions if necessary
+  $comment['deductions'] = isset($comment['deductions']) ? _time_tracker_round(_time_tracker_parse_duration($comment['deductions']), $rounding_interval / 60, $rounding_operation) : NULL;
+  // Check First if we are tracking by duration or time interval
+  if (variable_get('time_entry_method', 'duration') == 'duration') {
+    $duration = _time_tracker_round(_time_tracker_parse_duration($comment['duration']), $rounding_interval / 60, $rounding_operation);
+    if ($duration > 0) {
+      $comment['duration'] = $duration;
+      $comment['timestamp'] = strtotime($comment['time']);
+      unset($comment['time']);
+      // Update or create the time entry
+      $result = db_merge('time_tracker_entry')
+                ->key(array('teid' => $comment['teid'] ? 'teid' : array()))
+                ->fields(array(
+                  'deductions' => $comment['deductions'],
+                  'duration' => $comment['duration'],
+                  'timestamp' => $comment['timestamp'],
+                ))
+                ->execute();
+      return $result;  
+    }
+    elseif ($comment['teid']) {
+      // No duration, but there WAS duration information -- someone wants
+      // to remove the time entry!
+      db_delete('time_tracker_entry')
+        ->condition('teid', $comment['teid'])
+        ->execute();
+    }
+  }
+  elseif (variable_get('time_entry_method', 'duration') == 'interval') {
+    if (strlen($comment['start']) && strlen($comment['end'])) {
+      $comment['start'] = $comment['timestamp'] = _time_tracker_round(strtotime($comment['start']), $rounding_interval * 60, $rounding_operation);
+      $comment['end'] = _time_tracker_round(strtotime($comment['end']), $rounding_interval * 60, $rounding_operation);
+      //Do we make the duration into a timestamp now??
+      $comment['duration'] = _time_tracker_parse_duration(_time_tracker_convert_phptime_to_duration( $comment['start'], $comment['end']));
+
+      $result = db_merge('time_tracker_entry')
+                ->key(array('teid' => $comment['teid'] ? 'teid' : array()))
+                ->fields(array(
+                  'start' => $comment['start'],
+                  'end' => $comment['end'],
+                  'duration' => $comment['duration'],
+                ))
+                ->execute();
+      return $result;
+    }
+    elseif ($comment['teid']) {
+      // No duration, but there WAS duration information -- someone wants
+      // to remove the time entry!
+      db_delete('time_tracker_entry')
+        ->condition('teid', $comment['teid'])
+        ->execute();
+    }
   }
 }
 
+/**
+ * Implements hook_comment_delete().
+ */
+function time_tracker_comment_delete($comment) {
+  db_delete('time_tracker_entry')
+    ->condition('cid', $comment->cid)
+    ->execute();
+}
+
 /***********************************************************************
  * TIME TRACKER -- TIME ENTRY FORM
  ***********************************************************************/
@@ -722,7 +818,7 @@ function time_tracker_comment(&$comment, $op) {
  *    'comment => cid (comment id)
  *
  */
-function time_tracker_time_entry_form($form_state, $type = 'node', $id = 0) {
+function time_tracker_time_entry_form($form, $form_state, $type = 'node', $id = 0) {
   // Current user
   global $user;
 
@@ -730,19 +826,20 @@ function time_tracker_time_entry_form($form_state, $type = 'node', $id = 0) {
   drupal_add_css(drupal_get_path('module', 'time_tracker') .'/css/time_tracker.css');
 
   // Our list of possible activities formatted as an option list for the select drop down
-  $activities = get_active_activities_options();
+  $activities = _time_tracker_get_active_activities_options();
 
   // Assume it isn't locked for now.
   $lock = FALSE;
 
+//Hide this fieldset, because we are putting it another elsewhere... for now.
   // The time tracker time entry form fieldset
-  $form['time_tracker'] = array(
-    '#type' => 'fieldset',
-    '#title' => t('Time Tracker'),
-    '#collapsible' => TRUE,
-    '#collapsed' => variable_get('time_tracker_default_collapsed', 0),
-    '#weight' => -9,
-  );
+  // $form['time_tracker'] = array(
+  //  '#type' => 'fieldset',
+  //  '#title' => t('Time Tracker'),
+  //  '#collapsible' => TRUE,
+  //  '#collapsed' => variable_get('time_tracker_default_collapsed', 0),
+  //  '#weight' => -9,
+  //);
 
   // Display a message about the rounding if it's on and messages are set to display
   if (variable_get('time_tracker_rounding_message', 0) && variable_get('time_tracker_rounding_interval', 0)) {
@@ -762,8 +859,8 @@ function time_tracker_time_entry_form($form_state, $type = 'node', $id = 0) {
   // If we are editing a comment...
   if ($type == 'comment' && $id) {
     // Gotta grab the comment's info if we're editing an old comment
-    $results = db_query("SELECT * FROM {time_tracker_entry} WHERE cid = %d", $id);
-    $time_tracker_data = db_fetch_object($results);
+    $results = db_query("SELECT * FROM {time_tracker_entry} WHERE cid = :cid", array(':cid' =>$id));
+    $time_tracker_data = $results->fetchObject();
     // Need a comment id for comment tracking
     $form['cid'] = array(
       '#type' => 'value',
@@ -771,8 +868,8 @@ function time_tracker_time_entry_form($form_state, $type = 'node', $id = 0) {
     );
   }
   elseif ($type == 'edit' && $id) {
-    $results = db_query("SELECT * FROM {time_tracker_entry} WHERE teid = %d", $id);
-    $time_tracker_data = db_fetch_object($results);
+    $results = db_query("SELECT * FROM {time_tracker_entry} WHERE teid = :teid", array(':teid' =>$id));
+    $time_tracker_data = $results->fetchObject();
     // The node this time entry belongs to
     $form['nid'] = array(
       '#type' => 'value',
@@ -791,6 +888,9 @@ function time_tracker_time_entry_form($form_state, $type = 'node', $id = 0) {
     $form['#redirect']['redirect'] = 'node/' . $time_tracker_data->nid;
   }
   elseif ($type == 'node' && $id) {
+    $results = db_query("SELECT * FROM {time_tracker_entry} WHERE nid = :nid", array(':nid' =>$id));
+    $time_tracker_data = $results->fetchObject();
+
     // Just need the nid for node time entries
     $form['nid'] = array(
       '#type' => 'value',
@@ -799,11 +899,11 @@ function time_tracker_time_entry_form($form_state, $type = 'node', $id = 0) {
   }
 
   // If there is a time tracker entry data object
-  if ($time_tracker_data) {
+  if (isset($time_tracker_data)) {
     // If it's an existing time entry, we need the teid
     $form['teid'] = array(
       '#type' => 'value',
-      '#value' => $time_tracker_data->teid,
+      //'#value' => $time_tracker_data->teid,
     );
     if (variable_get('allow_locked_time_entries', 0)) {
       if (user_access('administer time entries')) {
@@ -811,7 +911,7 @@ function time_tracker_time_entry_form($form_state, $type = 'node', $id = 0) {
           '#title' => t('Locked'),
           '#type' => 'checkbox',
           '#description' => 'Lock this time entry, preventing further editing',
-          '#default_value' => $time_tracker_data->locked ? TRUE : FALSE,
+          //'#default_value' => $time_tracker_data->locked ? TRUE : FALSE,
           '#weight' => 6,
         );
       }
@@ -830,29 +930,29 @@ function time_tracker_time_entry_form($form_state, $type = 'node', $id = 0) {
       }
     }
   }
-
-  $user_submit = user_load($time_tracker_data->uid);
-
+  //if (isset($time_tracker_data)) {
+  //  $user_submit = user_load($time_tracker_data->uid);
+  //}
   // Auto complete/select user reference but only if you have permission
   $form['time_tracker']['username'] = array(
     '#title' => t('User'),
     '#access' => user_access('administer time entries'),
     '#type' => $lock ? 'value' : variable_get('time_tracker_user_field_type', 'textfield'),
     '#weight' => 0,
-    '#default_value' => $user_submit->name ? $user_submit->name : $user->name,
+    //'#default_value' => $user_submit->name ? $user_submit->name : $user->name,
   );
 
   if(variable_get('time_tracker_user_field_type', 'textfield') == 'select'){
-    if (module_exists('casetracker')) {
-      $options = drupal_map_assoc(casetracker_user_options());
-    } else {
-      $users = db_query("SELECT uid, name FROM {users} WHERE uid > 0");
-
-      while ($user_info = db_fetch_array($users)) {
-        $options[$user_info['uid']] = $user_info['name'];
-      }
+    //if (module_exists('casetracker')) {
+    //  $options = drupal_map_assoc(casetracker_user_options());
+    //} else {
+    $users = db_query("SELECT uid, name FROM {users} WHERE uid > 0");
 
+    while ($user_info = db_fetch_array($users)) {
+      $options[$user_info['uid']] = $user_info['name'];
     }
+
+    //}
     array_shift($options);
     $form['time_tracker']['username'] += array('#options' => $options);
     $form['time_tracker']['username']['#weight'] -= 1;
@@ -870,19 +970,13 @@ function time_tracker_time_entry_form($form_state, $type = 'node', $id = 0) {
     '#type' => $lock ? 'value' : 'select',
     '#weight' => 0,
     '#options' => $activities,
-    '#default_value' => $time_tracker_data->activity,
+    //'#default_value' => $time_tracker_data->activity,
   );
 
   // Add some javascript and css for the datepicker
-  if (module_exists('jquery_ui')) {
-    jquery_ui_add(array('ui.datepicker'));
-    drupal_add_js(drupal_get_path('module', 'time_tracker') .'/js/datepicker.settings.js');
-    // $js_settings['timeTracker'] = array(
-    //   'timeTrackerDateFormat' => datepicker_format_replacements(variable_get('timestamp_date_format', 'F d, Y')),
-    // );
-    // drupal_add_js($js_settings, 'setting');
-    drupal_add_css(drupal_get_path('module', 'jquery_ui') .'/jquery.ui/themes/default/ui.datepicker.css');
-  }
+  drupal_add_library('system', 'ui.datepicker');    
+  drupal_add_js(drupal_get_path('module', 'time_tracker') .'/js/datepicker.settings.js');
+  
 
   // Insert different form elements depending on the time_entry_method
   if (variable_get('time_entry_method', 'duration') == 'duration') {
@@ -894,14 +988,14 @@ function time_tracker_time_entry_form($form_state, $type = 'node', $id = 0) {
       '#date_label_position' => 'within',
       '#size' => 20,
       '#weight' => 0,
-      '#default_value' => empty($time_tracker_data->timestamp) ? date(TIME_TRACKER_DEFAULT_DATE_FORMAT, time()) : date(TIME_TRACKER_DEFAULT_DATE_FORMAT, $time_tracker_data->timestamp),
+      //'#default_value' => empty($time_tracker_data->timestamp) ? date(TIME_TRACKER_DEFAULT_DATE_FORMAT, time()) : date(TIME_TRACKER_DEFAULT_DATE_FORMAT, $time_tracker_data->timestamp),
     );
     $form['time_tracker']['time_entry']['duration'] = array(
       '#title' => t('Hours'),
       '#type' => $lock ? 'value' : 'textfield',
       '#size' => '10',
       '#weight' => 1,
-      '#default_value' => $time_tracker_data->duration ? format_hours_to_hours_and_minutes($time_tracker_data->duration, TRUE) : $time_tracker_data->duration,
+      //'#default_value' => $time_tracker_data->duration ? _time_tracker_format_hours_to_hours_and_minutes($time_tracker_data->duration, TRUE) : $time_tracker_data->duration,
       '#description' => t('eg. 2.5 or 2:30 for two and a half hours'),
     );
   }
@@ -914,7 +1008,7 @@ function time_tracker_time_entry_form($form_state, $type = 'node', $id = 0) {
     $form['time_tracker']['time_entry']['start'] = array(
       '#title' => t('Start'),
       '#type' => $lock ? 'value' : 'date_popup',
-      '#default_value' => $time_tracker_data->start ? date(TIME_TRACKER_DEFAULT_DATE_FORMAT, $time_tracker_data->start) : '',
+     // '#default_value' => $time_tracker_data->start ? date(TIME_TRACKER_DEFAULT_DATE_FORMAT, $time_tracker_data->start) : '',
       '#date_format' => $format,
       '#date_label_position' => 'within',
       '#weight' => 1,
@@ -922,7 +1016,7 @@ function time_tracker_time_entry_form($form_state, $type = 'node', $id = 0) {
     $form['time_tracker']['time_entry']['end'] = array(
       '#title' => t('End'),
       '#type' => $lock ? 'value' : 'date_popup',
-      '#default_value' => $time_tracker_data->end ? date(TIME_TRACKER_DEFAULT_DATE_FORMAT, $time_tracker_data->end) : '',
+     // '#default_value' => $time_tracker_data->end ? date(TIME_TRACKER_DEFAULT_DATE_FORMAT, $time_tracker_data->end) : '',
       '#date_format' => $format,
       '#date_label_position' => 'within',
       '#weight' => 2,
@@ -932,13 +1026,15 @@ function time_tracker_time_entry_form($form_state, $type = 'node', $id = 0) {
       '#weight' => 3,
     );
     // If duration data exists already we post a note to the user
-    if ($time_tracker_data->duration && !($time_tracker_data->end) && !($time_tracker_data->start)) {
-      $form['time_tracker']['duration_msg'] = array(
-        '#prefix' => '<div class="description"><em>',
-        '#value' => t("A duration value exists for this time entry, but no Start and End time. <br/> Saving this entry with a Start and End time will overwrite the duration<br/> Logged Duration: <b>!duration</b>", array('!duration' => format_hours_to_hours_and_minutes($time_tracker_data->duration))),
-        '#suffix' => '</em></div>',
-        '#weight' => 4,
-      );
+    if (isset($time_tracker_data)) {
+      if ($time_tracker_data->duration && !($time_tracker_data->end) && !($time_tracker_data->start)) {
+        $form['time_tracker']['duration_msg'] = array(
+          '#prefix' => '<div class="description"><em>',
+          '#value' => t("A duration value exists for this time entry, but no Start and End time. <br/> Saving this entry with a Start and End time will overwrite the duration<br/> Logged Duration: <b>!duration</b>", array('!duration' => _time_tracker_format_hours_to_hours_and_minutes($time_tracker_data->duration))),
+          '#suffix' => '</em></div>',
+          '#weight' => 4,
+        );
+      }
     }
   }
 
@@ -949,7 +1045,7 @@ function time_tracker_time_entry_form($form_state, $type = 'node', $id = 0) {
       '#type' => $lock ? 'value' : 'textfield',
       '#size' => '10',
       '#weight' => 3,
-      '#default_value' => $time_tracker_data->deductions ? format_hours_to_hours_and_minutes($time_tracker_data->deductions, TRUE) : $time_tracker_data->deductions,
+      //'#default_value' => $time_tracker_data->deductions ? _time_tracker_format_hours_to_hours_and_minutes($time_tracker_data->deductions, TRUE) : $time_tracker_data->deductions,
       '#description' => t('eg. 2.5 or 2:30 for two and a half hours'),
     );
   }
@@ -961,7 +1057,7 @@ function time_tracker_time_entry_form($form_state, $type = 'node', $id = 0) {
       '#type' => $lock ? 'value' : 'checkbox',
       '#size' => '10',
       '#weight' => 6,
-      '#default_value' => $time_tracker_data->billable,
+      //'#default_value' => $time_tracker_data->billable,
     );
   }
   if (variable_get('enable_billed_field', 0)) {
@@ -970,7 +1066,7 @@ function time_tracker_time_entry_form($form_state, $type = 'node', $id = 0) {
       '#type' => $lock ? 'value' : 'checkbox',
       '#size' => '10',
       '#weight' => 6,
-      '#default_value' => $time_tracker_data->billed,
+      //'#default_value' => $time_tracker_data->billed,
     );
   }
 
@@ -982,7 +1078,7 @@ function time_tracker_time_entry_form($form_state, $type = 'node', $id = 0) {
       '#weight' => 7,
       '#rows' => 2,
       '#resizable' => FALSE,
-      '#default_value' => $time_tracker_data->note,
+      //'#default_value' => $time_tracker_data->note,
     );
     if (!$lock) {
       $form['time_tracker']['submit'] = array(
@@ -1052,7 +1148,7 @@ function time_tracker_time_entry_submit($form, &$form_state) {
   // Our submitted form values
   $values = $form_state['values'];
   // Need a user
-  $user = user_load(array('name' => $values['username']));
+  $user = user_load_by_name($values['username']);
   // If this is an existing time entry...
   $time['teid'] = $values['teid'];
   // Store the rounding data
@@ -1063,14 +1159,14 @@ function time_tracker_time_entry_submit($form, &$form_state) {
   if (variable_get('time_entry_method', 'duration') == 'duration') {
     $time['start'] = 0;
     $time['end'] = 0;
-    $time['duration'] = time_tracker_round(_time_tracker_parse_duration($values['duration']), $rounding_interval / 60, $rounding_operation);
+    $time['duration'] = _time_tracker_round(_time_tracker_parse_duration($values['duration']), $rounding_interval / 60, $rounding_operation);
     $time['timestamp'] = strtotime($values['time']);
   }
   elseif (variable_get('time_entry_method', 'duration') == 'interval') {
-    $time['start'] = time_tracker_round(strtotime($values['start']), $rounding_interval * 60, $rounding_operation);
-    $time['end'] = time_tracker_round(strtotime($values['end']), $rounding_interval * 60, $rounding_operation);
+    $time['start'] = _time_tracker_round(strtotime($values['start']), $rounding_interval * 60, $rounding_operation);
+    $time['end'] = _time_tracker_round(strtotime($values['end']), $rounding_interval * 60, $rounding_operation);
     $time['timestamp'] = $time['start'];
-    $time['duration'] = _time_tracker_parse_duration(convert_phptime_to_duration($time['start'], $time['end']));
+    $time['duration'] = _time_tracker_parse_duration(_time_tracker_convert_phptime_to_duration($time['start'], $time['end']));
   }
 
   // The rest of the time entry
@@ -1080,35 +1176,74 @@ function time_tracker_time_entry_submit($form, &$form_state) {
   $time['note'] = $values['note'];
 
   // If locked was in the form use it
-  $time['locked'] = isset($values['locked']) ? $values['locked'] : FALSE;
+  $time['locked'] = isset($values['locked']) ? $values['locked'] : 0;
   // If billable was in the form use it
   $time['billable'] = isset($values['billable']) ? $values['billable'] : NULL;
   // If billed was in the form use it
   $time['billed'] = isset($values['billed']) ? $values['billed'] : NULL;
   // If billed was in the form use it
-  $time['deductions'] = isset($values['deductions']) ? time_tracker_round(_time_tracker_parse_duration($values['deductions']), $rounding_interval / 60, $rounding_operation) : NULL;
+  $time['deductions'] = isset($values['deductions']) ? _time_tracker_round(_time_tracker_parse_duration($values['deductions']), $rounding_interval / 60, $rounding_operation) : NULL;
 
   // Update the db and send a message to the user
-  drupal_write_record('time_tracker_entry', $time, $time['teid'] ? 'teid' : array());
-
-  // Message to the user
-  if ($rounding_interval) {
-    if ($rounding_operation == 'auto') {
-      $rounding_operation = 'rounded';
+  if($time['teid']){
+    $result = db_merge('time_tracker_entry')
+              ->key(array('teid' => $time['teid']))
+              ->fields(array(
+                'start' => $time['start'],
+                'end' => $time['end'],
+                'duration' => $time['duration'],
+                'timestamp' => $time['timestamp'],
+                'activity' => $time['activity'],
+                'uid' => $time['uid'],
+                'nid' => $time['nid'],
+                'note' => $time['note'],
+                'locked' => $time['locked'],
+                'billable' => $time['billable'],
+                'billed' => $time['billed'],
+                'deductions' => $time['deductions'],
+              ))
+              ->execute();
+  }
+  elseif($time['duration']){
+    $result = db_insert('time_tracker_entry')
+              ->fields(array(
+                'start' => $time['start'],
+                'end' => $time['end'],
+                'duration' => $time['duration'],
+                'timestamp' => $time['timestamp'],
+                'activity' => $time['activity'],
+                'uid' => $time['uid'],
+                'nid' => $time['nid'],
+                'note' => $time['note'],
+                'locked' => $time['locked'],
+                'billable' => $time['billable'],
+                'billed' => $time['billed'],
+                'deductions' => $time['deductions'],
+              ))
+              ->execute();
+  }
+  // Message to the user, only if we actually had something to record.
+  if(isset($result)){
+    if ($rounding_interval) {
+      if ($rounding_operation == 'auto') {
+        $rounding_operation = 'rounded';
+      }
+      else {
+        $rounding_operation = 'rounded ' . $rounding_operation;
+      }
+      drupal_set_message(t('Time Entry Recorded. Time was !rounded to the nearest !minutes mintues', array('!rounded' => $rounding_operation, '!minutes' => $rounding_interval)));
     }
     else {
-      $rounding_operation = 'rounded ' . $rounding_operation;
+      drupal_set_message(t('Time Entry Recorded'));
     }
-    drupal_set_message(t('Time Entry Recorded. Time was !rounded to the nearest !minutes mintues', array('!rounded' => $rounding_operation, '!minutes' => $rounding_interval)));
-  }
-  else {
-    drupal_set_message(t('Time Entry Recorded'));
   }
 }
 
 function time_tracker_time_entry_delete($form, &$form_state) {
   if ($form_state['values']['teid']) {
-    db_query("DELETE FROM {time_tracker_entry} WHERE teid = %d", $form_state['values']['teid']);
+    db_delete('time_tracker_entry')
+      ->condition('teid', $form_state['values']['teid'])
+      ->execute();
   }
   drupal_set_message(t('Time Entry Deleted'));
 }
@@ -1128,7 +1263,7 @@ function time_tracker_theme() {
     'time_tracker_project_summary' => array(), // not in use
     'time_tracker_summary' => array(),
     'time_tracker_activity_table' => array(
-      'arguments' => array('form' => array()),
+      'render element' => 'form',
     ),
   );
 }
@@ -1210,11 +1345,11 @@ function theme_time_tracker_time_entry_table($time_entries) {
             'class' => 'time_entry_username',
           ),
           array( //Cell2
-            'data' => get_activity_name($time_entry->activity),
+            'data' => _time_tracker_get_activity_name($time_entry->activity),
             'class' => 'time_entry_activity',
           ),
           array( //Cell3
-            'data' => format_hours_to_hours_and_minutes($time_entry->duration),
+            'data' => _time_tracker_format_hours_to_hours_and_minutes($time_entry->duration),
             'class' => 'time_entry_duration',
           ),
         ),
@@ -1222,11 +1357,11 @@ function theme_time_tracker_time_entry_table($time_entries) {
       );//endrow
       if (variable_get('enable_deductions_field', 0)) {
         $row['data'][] = array( //Cell6
-   	      'data' => format_hours_to_hours_and_minutes($time_entry->deductions),
+   	      'data' => _time_tracker_format_hours_to_hours_and_minutes($time_entry->deductions),
    	      'class' => 'time_entry_deductions',
    	    );
    	    $row['data'][] = array( //Cell6
-   	      'data' => format_hours_to_hours_and_minutes($time_entry->duration - $time_entry->deductions),
+   	      'data' => _time_tracker_format_hours_to_hours_and_minutes($time_entry->duration - $time_entry->deductions),
    	      'class' => 'time_entry_total',
    	    );
       }
@@ -1270,6 +1405,8 @@ function theme_time_tracker_time_entry_table($time_entries) {
       $rows[] = $row;
     }
   }
+
+
   $form = array();
   $form['time_tracker_time_entries'] = array(
     '#type' => 'fieldset',
@@ -1277,8 +1414,14 @@ function theme_time_tracker_time_entry_table($time_entries) {
     '#collapsible' => TRUE,
     '#collapsed' => variable_get('time_entry_table_default_collapsed', 0),
   );
+
+  $table = array(
+  	'header' => $header,
+        'rows' => $rows,
+        'attributes' => array('class' => array('time_tracker_time_entries')),
+  );
   $form['time_tracker_time_entries']['entry_table'] = array(
-    '#value' => theme('table', $header, $rows, array('class' => 'time_tracker_time_entries')),
+    '#markup' => theme('table', $table),
   );
   return drupal_render($form);
 }
@@ -1308,7 +1451,7 @@ function theme_time_tracker_comment($time_entry) {
     $rows = array();
     $time = '';
 
-    $activity = get_activity_name($time_entry->activity);
+    $activity = _time_tracker_get_activity_name($time_entry->activity);
     if ($activity) {
       $activity = '(' . $activity . ') ';
     }
@@ -1334,7 +1477,7 @@ function theme_time_tracker_comment($time_entry) {
       $row = array(//row
         'data' => array( //row data
           array( //Cell3
-            'data' => format_hours_to_hours_and_minutes($time_entry->duration),
+            'data' => _time_tracker_format_hours_to_hours_and_minutes($time_entry->duration),
             'class' => 'time_entry_duration',
           ),
         ),
@@ -1342,16 +1485,16 @@ function theme_time_tracker_comment($time_entry) {
       );//endrow
       if (variable_get('enable_deductions_field', 0)) {
         $row['data'][] = array(
-   	      'data' => format_hours_to_hours_and_minutes($time_entry->deductions),
+   	      'data' => _time_tracker_format_hours_to_hours_and_minutes($time_entry->deductions),
    	      'class' => 'time_entry_deductions',
    	    );
    	    $row['data'][] = array(
-   	      'data' => format_hours_to_hours_and_minutes($time_entry->duration - $time_entry->deductions),
+   	      'data' => _time_tracker_format_hours_to_hours_and_minutes($time_entry->duration - $time_entry->deductions),
    	      'class' => 'time_entry_total',
    	    );
       }
       $row['data'][] = array(
-        'data' => get_activity_name($time_entry->activity),
+        'data' => _time_tracker_get_activity_name($time_entry->activity),
         'class' => 'time_entry_activity',
  	    );
  	    $row['data'][] = array(
@@ -1375,13 +1518,18 @@ function theme_time_tracker_comment($time_entry) {
    	    );
       }
       $rows[] = $row;
-      return theme('table', $header, $rows, array('class' => 'time_tracker_entry_comment'));
+      $table = array(
+      	'header' => $header,
+        'rows' => $rows,
+        'attributes' => array('class' => array('time_tracker_entry_comment')),
+      );
+      return theme('table', $table);
     }
     else {
       $time_string = array();
-      $time_string['total'] = format_hours_to_hours_and_minutes($time_entry->duration - $time_entry->deductions);
+      $time_string['total'] = _time_tracker_format_hours_to_hours_and_minutes($time_entry->duration - $time_entry->deductions);
       if (variable_get('enable_deductions_field', 0)) {
-        $time_string['total_details'] = '(' . format_hours_to_hours_and_minutes($time_entry->duration) . ' - ' . format_hours_to_hours_and_minutes($time_entry->deductions) . ')';
+        $time_string['total_details'] = '(' . _time_tracker_format_hours_to_hours_and_minutes($time_entry->duration) . ' - ' . _time_tracker_format_hours_to_hours_and_minutes($time_entry->deductions) . ')';
       }
       $time_string['activity'] = $activity;
       $time_string['on'] = t('on');
@@ -1395,8 +1543,12 @@ function theme_time_tracker_comment($time_entry) {
 
       $rows[] = array(t('Time') . ': ' . implode(' ', $time_string));
 
+      $table = array(
+        'rows' => $rows,
+        'attributes' => array('class' => array('time_tracker_entry_comment')),
+      );
 
-      return theme('table', NULL, $rows, array('class' => 'time_tracker_entry_comment'));
+      return theme('table', $table);
     }
   }
 }
@@ -1414,13 +1566,23 @@ function theme_time_tracker_summary($total_time) {
   $rows = array();
 
   if ($total_time > 0) {
-    $rows[] = array(t('Time logged: ') . '<span class="time_tracker_time">' .
-                    format_hours_to_hours_and_minutes($total_time, FALSE, TRUE) . '</span>');
+    $rows[] = array(
+	t('Time logged: '),
+	'<span class="time_tracker_time">' . _time_tracker_format_hours_to_hours_and_minutes($total_time, FALSE, TRUE) . '</span>',
+    );
   }
+  $table = array(
+  	'rows' => $rows,
+        'attributes' => array('class' => array('time_tracker_summary')),
+  );
+
 
-  return theme('table', NULL, $rows, array('class' => 'time_tracker_summary'));
+  return theme('table', $table);
 }
 
+
+
+
 /**
  * Theme function for case_tracker projects.
  *
@@ -1432,9 +1594,9 @@ function theme_time_tracker_summary($total_time) {
  * @return
  *    A themed table of summary data
  */
-function theme_time_tracker_project_summary($total_time) {
+//function theme_time_tracker_project_summary($total_time) {
   // COPY OF theme_time_tracker_summary() ...
-}
+//}
 
 
 /**
@@ -1480,7 +1642,7 @@ function time_tracker_get_time_entries_for_node($nid, $type = 'all') {
       // Get the entries associated with this node
       $resource = db_query($sql, $nid);
       // Store the db objects in an array for theme function
-      while ($time_entry = db_fetch_object($resource)) {
+      while ($time_entry = $resource->fetchObject()) {
         $time_entries[] = $time_entry;
       }
       break;
@@ -1491,7 +1653,7 @@ function time_tracker_get_time_entries_for_node($nid, $type = 'all') {
       // Get the entries associated with this node
       $resource = db_query($sql, $nid);
       // Store the db objects in an array for theme function
-      while ($time_entry = db_fetch_object($resource)) {
+      while ($time_entry = $resource->fetchObject()) {
         $time_entries[] = $time_entry;
       }
       breal;
@@ -1502,7 +1664,7 @@ function time_tracker_get_time_entries_for_node($nid, $type = 'all') {
       // Get the entries associated with this node
       $resource = db_query($sql, $nid);
       // Store the db objects in an array for theme function
-      while ($time_entry = db_fetch_object($resource)) {
+      while ($time_entry = $resource->fetchObject()) {
         $time_entries[] = $time_entry;
       }
       break;
@@ -1524,7 +1686,6 @@ function time_tracker_get_time_entries_for_node($nid, $type = 'all') {
  */
 function time_tracker_get_enabled_fields($type = 'node') {
   $enabled_fields = array();
-
   return $enabled_fields;
 }
 
@@ -1563,9 +1724,9 @@ function time_tracker_is_tracking_time($node_type) {
  * @return
  *    The name of the activity
  */
-function get_activity_name($activity_id) {
-  $result = db_query("SELECT name FROM {time_tracker_activity} WHERE taid = '". $activity_id ."'");
-  $activity = db_fetch_object($result);
+function _time_tracker_get_activity_name($activity_id) {
+  $result = db_query("SELECT name FROM {time_tracker_activity} WHERE taid = :taid", array(':taid' => $activity_id));
+  $activity = $result->fetchObject();
 
   return $activity->name;
 }
@@ -1580,15 +1741,15 @@ function get_activity_name($activity_id) {
  *    Returns the total logged time ona node. Includes both
  *    comment and node based time entries
  */
-function time_tracker_get_total_logged_time($nid) {
+function _time_tracker_get_total_logged_time($nid) {
 
   // Initialize the $total_time variable to 0
   $total_time = 0;
 
   // if this is a node form display the time tracker
-  $resource = db_query("SELECT * FROM {time_tracker_entry} WHERE nid = %d", $nid);
+  $resource = db_query("SELECT * FROM {time_tracker_entry} WHERE nid = :nid", array(':nid' => $nid));
 
-  while ($time_entry = db_fetch_object($resource)) {
+  while ($time_entry = $resource->fetchObject()) {
     $total_time = $total_time + ($time_entry->duration - $time_entry->deductions);
   }
 
@@ -1622,7 +1783,7 @@ function time_tracker_get_total_logged_time($nid) {
  *    e.g. 10 hours, 30 minutes
  *
  */
-function format_hours_to_hours_and_minutes($hours, $clock_time = FALSE, $abbrev_hrs = FALSE, $abbrev_mins = TRUE, $sep = ', ') {
+function _time_tracker_format_hours_to_hours_and_minutes($hours, $clock_time = FALSE, $abbrev_hrs = FALSE, $abbrev_mins = TRUE, $sep = ', ') {
   $hrs = floor($hours);
   $mins = round(($hours - $hrs) * 60);
   if ($mins == 60) {
@@ -1675,7 +1836,7 @@ function format_hours_to_hours_and_minutes($hours, $clock_time = FALSE, $abbrev_
  * @param $stop
  *    The end time, in seconds
  */
-function convert_phptime_to_duration($start, $stop = 0) {
+function _time_tracker_convert_phptime_to_duration($start, $stop = 0) {
   if ($stop == 0) {
     $duration = $start;
   }
@@ -1723,7 +1884,7 @@ function convert_phptime_to_duration($start, $stop = 0) {
  * @return
  *   The rounded timestamp
  */
-function time_tracker_round($value, $interval, $operation = 'auto') {
+function _time_tracker_round($value, $interval, $operation = 'auto') {
 
   // Make sure it's a numeric value and the interval isn't 0
   if (!is_numeric($value) || $interval <= 0) {
@@ -1749,11 +1910,11 @@ function time_tracker_round($value, $interval, $operation = 'auto') {
  * Helper function to get the active activities
  * Formatted as a options list array for a select form element
  */
-function get_active_activities_options() {
+function _time_tracker_get_active_activities_options() {
   $activities = array();
   $activities[] = '';
   $results = db_query("SELECT * FROM {time_tracker_activity} WHERE status = 1 ORDER BY weight ASC");
-  while ($result = db_fetch_object($results)) {
+  while ($result = $results->fetchObject()) {
     $activities[$result->taid] = $result->name;
   }
   return $activities;
@@ -1793,4 +1954,37 @@ function _time_tracker_parse_duration($input) {
   return $hours;
 }
 
+/**
+ * Implements hook_action_info().
+ */
+function time_tracker_action_info() {
+  return array(
+    'time_tracker_billed_action' => array(
+      'label' => t('Mark entries as billed'),
+      'type' => 'time_tracker',
+      'configurable' => FALSE,
+      'triggers' => array('time_tracker_billed'),
+    ),
+  );
+}
+
+function time_tracker_billed_action($teids, $context) {
+  foreach ($teids as $teid) {
+    db_update('time_tracker_entry')
+      ->fields(array(
+        'billed' => 1,
+      ))
+      ->condition('teid', $teid, '=')
+      ->execute();
+  }
+}
+
+
+
+
+
+
+
+
+
 
diff --git a/views/time_tracker.views.inc b/views/time_tracker.views.inc
index c207c6c..1d479a7 100644
--- a/views/time_tracker.views.inc
+++ b/views/time_tracker.views.inc
@@ -485,21 +485,6 @@ function time_tracker_views_bulk_operations_object_info() {
 }
 
 
-/**
- * Implementation of hook_action_info().
- */
-function time_tracker_action_info() {
-  return array(
-    'time_tracker_time_tracker_billed_action' => array(
-      'description' => t('Mark entries as billed'),
-      'type' => 'time_tracker',
-      'configurable' => FALSE,
-      'hooks' => array(
-        'time_tracker' => array('billed'),
-      ),
-    ),
-  );
-}
 
 
 /***********************************************************************
@@ -507,11 +492,7 @@ function time_tracker_action_info() {
  ***********************************************************************/
 
 
-function time_tracker_time_tracker_billed_action($teids, $context) {
-  foreach ($teids as $teid) {
-    db_query("UPDATE time_tracker_entry SET billed = 1 WHERE teid = %d", $teid);
-  }
-}
+
 
 function _time_tracker_entry_load($teid) {
   return db_fetch_object(db_query("SELECT *, n.title FROM {time_tracker_entry} AS te JOIN {node} AS n ON te.nid = n.nid WHERE teid = %d", $teid));
