diff --git a/upload_replace.module b/upload_replace.module
index 0489f2d..1665d13 100644
--- a/upload_replace.module
+++ b/upload_replace.module
@@ -1,4 +1,5 @@
 <?php
+
 /**
  * @file
  * A module file for providing functionality to replace files on upload
@@ -11,118 +12,129 @@
  *   be renamed to <filename>.<ext>
  */
 
-
 /**
- * Implementation of hook_file_update()
+ * Implements hook_file_update().
  */
-function upload_replace_file_update(&$new_file) {
+function upload_replace_file_update($new_file) {
+
   if (!$new_file->fid) {
-    //Nothing to do if no fileid
+    // Nothing to do if no file id
     return;
   }
   
   $desired_destination = preg_replace('/_[0-9]+\.(.*)$/', '.$1', $new_file->uri);
+
   $db_path = db_select('file_managed', 'f')
-    ->fields('f', array('uri', ))
+    ->fields('f', array('uri'))
     ->condition('fid', $new_file->fid)
     ->execute()
-    ->fetchField();
-  if ($db_path != $new_file->uri) {
-    //this happens when a reversion is being reverted
-    $next_good_filepath = file_destination($desired_destination, FILE_EXISTS_RENAME);
+    ->fetchAssoc();
+
+  if ($db_path['uri'] != $new_file->uri) {
+
+    // This happens when a reversion is being reverted.
+    $next_good_uri = file_destination($desired_destination, FILE_EXISTS_RENAME);
     db_update('file_managed')
-      ->fields(array('uri' => $next_good_filepath))
+      ->fields(array('uri' => $next_good_uri))
       ->condition('fid', $new_file->fid)
       ->execute();
+
     $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->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
+
+    // If the filename has be modified by adding a _0 value, or
+    // on certain situations the uri will not match the uri in the db, such as
+    // when reverting a revision. When reverting a revision change the filename as well.
+    if (!strpos($new_file->uri, $new_file->filename)) {
+
+      // The filename is not in the uri, 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('file_managed', 'f')
         ->fields('f')
         ->condition('uri', $desired_destination)
         ->execute();
-      //@todo only one result is handled, should allow for multiple results
-      $is_blocked = false;
-      
+
+      // @TODO only one result is handled, should allow for multiple results
       foreach ($result as $file) {
         $is_blocked = TRUE;
         $blocking_file = $file;
-        $tmp_destination = file_directory_temp() ."/$blocking_file->filename";
+        $tmp_destination = file_directory_temp() . "/test_-" . $blocking_file->fid . "_-" . $blocking_file->filename;
       }
 
-      $old_destination = $db_path;
+      $old_destination = $db_path['uri'];
 
-      if ($old_destination == $desired_destination){
-        return;
-      }
-      //Swap the files
+      $t_message = 'The file %old could not be moved to %new';
+      
+      // Swap the files.
       if ($is_blocked) {
-        //move the blocking file to a temporary location
+
+        // Move the blocking file to a temparary 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');
+          drupal_set_message(t($t_message, 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
+
+        // Move blocking file was successful, update the DB
         db_update('file_managed')
           ->fields(array('uri' => $tmp_destination))
           ->condition('fid', $blocking_file->fid)
           ->execute();
-      }
 
+      }
 
-      //move the newfile to the prefered location
+      // Move the newfile to the prefered location
       if (!file_unmanaged_move($old_destination, $desired_destination)) {
-        drupal_set_message(t('The file %old could not be moved to %new', array('%old' => $old_destination, '%new' => $desired_destination)), 'error');
+        drupal_set_message(t($message, array('%old' => $old_destination, '%new' => $desired_destination)), 'error');
         return;
       }
-      //move newfile was successful, update the DB
+
+      // Move newfile was successful, update the DB
       db_update('file_managed')
         ->fields(array('uri' => $desired_destination))
         ->condition('fid', $new_file->fid)
         ->execute();
-      $new_file->uri = $desired_destination;//set the newfile's path to the correct path
 
+      // Set the newfile's path to the correct path
+      $new_file->uri = $desired_destination;
 
       if ($is_blocked) {
-        //move the older file from temp to the new _0 location
+
+        // Move the older file from temp to the new _X location
         if (!file_unmanaged_move($tmp_destination, $old_destination)) {
-          drupal_set_message(t('The file %old could not be moved to %new', array('%old' => $tmp_destination, '%new' => $old_destination)), 'error');
+          drupal_set_message(t($t_message, array('%old' => $tmp_destination, '%new' => $old_destination)), 'error');
           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
+
+        // 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('file_managed')
           ->fields(array('uri' => $old_destination))
           ->condition('fid', $blocking_file->fid)
           ->execute();
+
       }
+
     }
+
   }
-  //Have to clear the cache because the revision data is cached somewhere
-  /*
-   * Find the nids where this file is used
-  $query = "SELECT DISTINCT nid FROM {files} WHERE fid=%d";
-  $result = db_query($query, $new_file->fid);
-  while($data = db_fetch_object($result)) {
-    cache_clear_all("content:$data->nid");
-  }
-  */
-  //This is inefficent, but how can we determine what nodes use this file?
- // cache_clear_all('*', 'cache_content', TRUE);
+
 }
 
 /**
- * HOOK_file_delete, update the filepath in the file object before deleting as we may have altered it above
- * @param object $new_file
+ * Implements hook_file_delete().
+ *
+ * Update the uri in the file object before deleting as we may have altered it above.
  */
-/*
-function upload_replace_file_delete(&$file) {
-  $file->filepath = db_result(db_query("SELECT filepath FROM {files} WHERE fid = %d", $file->fid));
+function upload_replace_file_delete($file) {
+
+  $file_id = db_select('file_managed', 'f')
+    ->fields('f', array('uri'))
+    ->condition('f.fid', $file->fid)
+    ->execute()
+    ->fetchField();
+
+  $file->uid = $file_id;
+
 }
-*/
\ No newline at end of file
