diff --git a/taxonomy_image.install b/taxonomy_image.install
index b803e42..f45db67 100644
--- a/taxonomy_image.install
+++ b/taxonomy_image.install
@@ -1,17 +1,43 @@
 <?php
 
 /**
+ * Implements hook_uninstall().
+ */
+function taxonomy_image_uninstall() {
+  db_delete('variable')
+    ->condition('name', 'taxonomy_image_%', 'LIKE')
+    ->execute();
+}
+
+/**
+ * Implements hook_update_dependencies().
+ */
+function taxonomy_image_update_dependencies() {
+  // Dependencies taken from Ubercart Catalog module	
+  $dependencies['taxonomy_image'][7000] = array(
+    'system' => 7020,
+    'taxonomy' => 7002,
+  );
+  $dependencies['taxonomy_image'][7001] = array(
+    'system' => 7059,
+  );
+  return $dependencies;
+}
+
+/**
  * @file
  * Update functions for the taxonomy_image module.
  */
-
 /**
  * Upgrades field data from the Drupal 6 version.
  */
-function taxonomy_image_update_7000(&$context) {
+function taxonomy_image_update_7000(&$sandbox) {
   if (!db_table_exists('term_image')) {
     return;
   }
+  if (!module_exists('image')) {
+    module_enable(array('file', 'image'), FALSE);
+  }
 
   $query = db_select('term_image', 't')
     ->fields('t', array('tid', 'path'));
@@ -22,23 +48,26 @@ function taxonomy_image_update_7000(&$context) {
     $sandbox['max'] = $query->countQuery()->execute()->fetchField();
     $sandbox['last_tid'] = -1;
 
-    field_create_field(array(
-      'field_name' => 'taxonomy_image',
-      'type' => 'image',
-      'cardinality' => 1,
-      'translatable' => TRUE,
-      'entity_types' => array('taxonomy_term'),
-    ));
+    if (!field_info_field('taxonomy_image')) {
+      field_create_field(array(
+        'field_name' => 'taxonomy_image',
+        'type' => 'image',
+        'cardinality' => 1,
+        'translatable' => TRUE,
+        'entity_types' => array('taxonomy_term'),
+      ));
+    }
   }
 
   // Select ten rows at a time.
   $result = $query
+    ->orderBy('tid', 'ASC')
     ->range(0, 10)
     ->condition('tid', $sandbox['last_tid'], '>')
     ->execute();
 
   // Upgrade each row.
-  while($row = $result->fetchAssoc()) {
+  while ($row = $result->fetchAssoc()) {
     $rows_exist = TRUE;
 
     // Load the term.
@@ -46,14 +75,15 @@ function taxonomy_image_update_7000(&$context) {
     if (!$term) {
       continue;
     }
+    $bundle = $term->vocabulary_machine_name;
 
     // Ensure the taxonomy_image field is instanciated.
-    if (!field_info_instance('taxonomy_term', 'taxonomy_image', $term->vocabulary_machine_name)) {
+    if (!field_info_instance('taxonomy_term', 'taxonomy_image', $bundle)) {
       field_create_instance(array(
         'field_name' => 'taxonomy_image',
         'entity_type' => 'taxonomy_term',
         'label' => t('Image'),
-        'bundle' => $term->vocabulary_machine_name,
+        'bundle' => $bundle,
         'description' => t('The image of this term.'),
         'required' => FALSE,
         'widget' => array(
@@ -61,34 +91,104 @@ function taxonomy_image_update_7000(&$context) {
         ),
       ));
     }
+  
+    $basename = variable_get('file_directory_path', conf_path() . '/files');
+    $scheme = variable_get('file_default_scheme', 'public') . '://';
+    $path = variable_get('taxonomy_image_path') ?  variable_get('taxonomy_image_path') .'/'. $row['path'] : $row['path'];
+    $files_path = $basename . '/' . $path;
+    
+    $files = db_query('SELECT fid, uid, filename, filepath AS uri, filemime, filesize, status, timestamp FROM {files} WHERE filepath = :path', array(':path' => $files_path))->fetchAllAssoc('fid', PDO::FETCH_ASSOC);
+
+    // if not in files - create one
+    if (!$files) {
+      // Make the file managed.
+      $file = array();
+      $file['uid'] = 1;
+      $file['filename'] = $row['path'];
+      $file['uri'] = $files_path;
+      $file['status'] = 1;
+      $uri = file_build_uri($path);
+      $file['filesize'] = filesize($uri);
+      $file['filemime'] = file_get_mimetype($uri);
+      $file['timestamp'] = time();
+      
+      //sometimes file does not exist...
+      if ($file['filesize']) {  
+        $files[] = $file;
+      }
+    }
+
+    foreach ($files as $file) {
+      // Get term id for the image.
+      $tid = $row['tid'];
+      // Don't break the insert query with extra data.
+   //   unset($file['tid']);
+      
+      // Drupal 5 upload module might have put multiple entries in file for 1 file
+      $file['uri'] = $scheme . str_replace($basename, '', $file['uri']);
+      $file['uri'] = file_stream_wrapper_uri_normalize($file['uri']);
+
+      $duplicate = db_select('file_managed', 'f')
+        ->fields('f', array('fid'))
+        ->condition('uri', $file['uri'])
+        ->execute()
+        ->fetchField();
+      
+      if ($duplicate && $duplicate != $file['fid']) {
+        $file['fid'] = $duplicate;
+      }
 
-    // Make the file managed.
-    $file = new stdClass();
-    $file->uri = file_build_uri($row['path']);
-    $file->status = FILE_STATUS_PERMANENT;
-    $file = file_save($file);
-
-    // Update the term with the new field data.
-    $term->taxonomy_image = array(
-      LANGUAGE_NONE => array(
-        array(
-          'fid' => $file->fid,
+      db_merge('file_managed')
+        ->key(array('fid' => $file['fid']))
+        ->fields(array(
+          'uid' => $file['uid'],
+          'filename' => $file['filename'],
+          'uri' => $file['uri'],
+          'filemime' => $file['filemime'],
+          'filesize' => $file['filesize'],
+          'status' => $file['status'],
+          'timestamp' => $file['timestamp'],
+        ))
+        ->execute();
+
+      // try to get the last insert ID
+      if (!$file['fid']) {
+        $file['fid'] = db_select('file_managed', 'f')
+          ->fields('f', array('fid'))
+          ->condition('uri', $file['uri'])
+          ->execute()
+          ->fetchField();
+      }
+        
+      // Add the usage entry for the file.
+      file_usage_add((object) $file, 'file', 'taxonomy_term', $tid);
+
+      $term = (object) array(
+        'tid' => $tid,
+        'taxonomy_image' => array(
+          LANGUAGE_NONE => array(
+            0 => array(
+              'fid' => $file['fid'],
+            ),
+          ),
         ),
-      ),
-    );
-    taxonomy_term_save($term);
+      );
+      _update_7000_field_sql_storage_write('taxonomy_term', $bundle, $term->tid, NULL, 'taxonomy_image', $term->taxonomy_image);
+    }
 
     // Increase counters.
     $sandbox['progress']++;
-    $sandbox['last_tid'] = $term->tid;
+    $sandbox['last_tid'] = $row['tid'];
+    $sandbox['message'] = check_plain($file['filename']);
   }
 
   // Finalize.
-  if (empty($rows_exist)) {
-    db_drop_table('term_image');
-    return t('Taxonomy image field data has been migrated. Settings must be adjusted manually.');
+  if ($sandbox['progress'] < $sandbox['max']) {
+    $sandbox['#finished'] = min(0.99, $sandbox['progress'] / $sandbox['max']);
   }
-  else {
-    $sandbox['#finished'] = $sandbox['progress'] / $sandbox['max'];
+  else {	 
+//    db_drop_table('term_image');
+    $sandbox['#finished'] = 1;
+    return t('Taxonomy image field data has been migrated. Settings must be adjusted manually.');
   }
 }
