diff --git a/upload_replace.module b/upload_replace.module
index 65d485b..0489f2d 100644
--- a/upload_replace.module
+++ b/upload_replace.module
@@ -20,33 +20,36 @@ function upload_replace_file_update(&$new_file) {
     //Nothing to do if no fileid
     return;
   }
-  $desired_destination = preg_replace('/_[0-9]+\.(.*)$/', '.$1', $new_file->filepath);
-  $db_path = db_select('files', 'f')
-    ->fields('f', array('filepath'))
+  
+  $desired_destination = preg_replace('/_[0-9]+\.(.*)$/', '.$1', $new_file->uri);
+  $db_path = db_select('file_managed', 'f')
+    ->fields('f', array('uri', ))
     ->condition('fid', $new_file->fid)
     ->execute()
     ->fetchField();
-  if ($db_path != $new_file->filepath) {
+  if ($db_path != $new_file->uri) {
     //this happens when a reversion is being reverted
     $next_good_filepath = file_destination($desired_destination, FILE_EXISTS_RENAME);
-    db_update('files')
-      ->fields(array('filepath' => $next_good_filepath))
+    db_update('file_managed')
+      ->fields(array('uri' => $next_good_filepath))
       ->condition('fid', $new_file->fid)
       ->execute();
-    $new_file->filepath = $desired_destination;
+    $new_file->uri = $desired_destination;
   }
   else {
     //If the filename has be modified by adding a _0 value, or
     //on certain situations the filepath will not match the filepath in the db, such as
     //when reverting a revision.  When reverting a revision change the filename as well
-    if (!strpos($new_file->filepath, $new_file->filename)) {
+    if (!strpos($new_file->uri, $new_file->uri)) {
       //the filename is not in the filepath, so drupal must have added a "_0" before the extension
       //find the file that is blocking this file from keeping the correct path
-      $result = db_select('files', 'f')
+      $result = db_select('file_managed', 'f')
         ->fields('f')
-        ->condition('filepath', $desired_destination)
+        ->condition('uri', $desired_destination)
         ->execute();
       //@todo only one result is handled, should allow for multiple results
+      $is_blocked = false;
+      
       foreach ($result as $file) {
         $is_blocked = TRUE;
         $blocking_file = $file;
@@ -54,17 +57,21 @@ function upload_replace_file_update(&$new_file) {
       }
 
       $old_destination = $db_path;
+
+      if ($old_destination == $desired_destination){
+        return;
+      }
       //Swap the files
       if ($is_blocked) {
-        //move the blocking file to a temparary location
+        //move the blocking file to a temporary location
         if (!file_unmanaged_move($desired_destination, $tmp_destination)) {
           drupal_set_message(t('The file %old could not be moved to %new', array('%old' => $desired_destination, '%new' => $tmp_destination)), 'error');
           return;
         }
         //DRUPAL 7 no longer changes the source filepath during move
         //move blocking file was successful, update the DB
-        db_update('files')
-          ->fields(array('filepath' => $tmp_destination))
+        db_update('file_managed')
+          ->fields(array('uri' => $tmp_destination))
           ->condition('fid', $blocking_file->fid)
           ->execute();
       }
@@ -76,11 +83,11 @@ function upload_replace_file_update(&$new_file) {
         return;
       }
       //move newfile was successful, update the DB
-      db_update('files')
-        ->fields(array('filepath' => $desired_destination))
+      db_update('file_managed')
+        ->fields(array('uri' => $desired_destination))
         ->condition('fid', $new_file->fid)
         ->execute();
-      $new_file->filepath = $desired_destination;//set the newfile's path to the correct path
+      $new_file->uri = $desired_destination;//set the newfile's path to the correct path
 
 
       if ($is_blocked) {
@@ -90,8 +97,8 @@ function upload_replace_file_update(&$new_file) {
           return;
         }
         //move blocking file was successful, update the DB with the actual location after file copy, so we use tmp_destination as it was updated during the move
-        db_update('files')
-          ->fields(array('filepath' => $old_destination))
+        db_update('file_managed')
+          ->fields(array('uri' => $old_destination))
           ->condition('fid', $blocking_file->fid)
           ->execute();
       }
