$md5 = trim($content[5]);
     $onetime = trim($content[6]);
   
-  if(!file_exists(VAR_FILES . $md5)) {
-    if (file_exists(VAR_FILES . $md5 . '_count')) {
-      unlink(VAR_FILES . $md5 . '_count');
-    }
-    unlink($link_file);
-    require(JIRAFEAU_ROOT . 'lib/template/header.php');
-    echo '<div class="error"><p>' . _('File not available.') . '</p></div>';
-    require(JIRAFEAU_ROOT . 'lib/template/footer.php');
-    exit;
-  }
+    if(!file_exists(VAR_FILES . $md5)) {
+      jirafeau_delete($link_name);
 
-  $counter = 1;
-  if (file_exists(VAR_FILES . $md5 . '_count')) {
-    $content = file(VAR_FILES . $md5 . '_count');
-    $counter = trim($content[0], NL);
-  }
+      require(JIRAFEAU_ROOT . 'lib/template/header.php');
+      echo '<div class="error"><p>' . _('File not available.') . '</p></div>';
+      require(JIRAFEAU_ROOT . 'lib/template/footer.php');
+      exit;
+    }
 
   if($time != JIRAFEAU_INFINITY) {
     if(time() > $time) {
-      unlink($link_file);
-
-      $counter--;
-      if ($counter >= 1) {
-        $handle = fopen(VAR_FILES . $md5 . '_count', 'w');
-        fwrite($handle, $counter);
-        fclose($handle);
-      }
-      elseif ($counter == 0) {
-        if (file_exists(VAR_FILES . $md5 . '_count')) {
-          unlink(VAR_FILES . $md5 . '_count');
-        }
-        $new_name = jirafeau_detect_collision($md5 . '_' . $file_name, VAR_TRASH);
-        rename(VAR_FILES . $md5, VAR_TRASH . $new_name);
-      }
+      jirafeau_delete($link_name);
 
       require(JIRAFEAU_ROOT . 'lib/template/header.php');
       echo '<div class="error"><p>' . _('The time limit of this file has expired. It has been deleted.') . '</p></div>';
     readfile(VAR_FILES . $md5);
 
     if($onetime == 'O') {
-      unlink($link_file);
-
-      $counter--;
-      if ($counter >= 1) {
-        $handle = fopen(VAR_FILES . $md5 . '_count', 'w');
-        fwrite($handle, $counter);
-        fclose($handle);
-      }
-      elseif ($counter == 0) {
-        if (file_exists(VAR_FILES . $md5 . '_count')) {
-          unlink(VAR_FILES . $md5 . '_count');
-        }
-        $new_name = jirafeau_detect_collision($md5 . '_' . $file_name, VAR_TRASH);
-        rename(VAR_FILES . $md5, VAR_TRASH . $new_name);
-      }
+      jirafeau_delete($link_name);
     }
     exit;
   } else {
 
 require(JIRAFEAU_ROOT . 'lib/functions.php');
 
 /* check if the destination dirs are writable */
-$writable = is_writable(VAR_FILES) && is_writable(VAR_LINKS) && is_writable(VAR_TRASH);
+$writable = is_writable(VAR_FILES) && is_writable(VAR_LINKS);
 
 $res = array();
 if($writable && isset($_POST['jirafeau'])) {
   add_error (_('The link directory is not writable!'), VAR_LINKS);
 }
 
-if(!is_writable(VAR_TRASH)) {
-  add_error (_('The trash directory is not writable!'), VAR_TRASH);
-}
-
 /* Check if the install.php script is still in the directory. */
 if (file_exists(JIRAFEAU_ROOT . 'install.php')) {
   add_error (_('Installer script still present'),
 
     );
   }
   
-  foreach(array('files', 'links', 'trash') as $subdir) {
+  foreach(array('files', 'links') as $subdir) {
     $subpath = $path . $subdir;
 
     if(!jirafeau_mkdir($subpath)) {
 
   return _('Unknown error.');
 }
 
+/** Remove link and it's file
+ * @param $link the link's name (hash)
+ */
+
+function jirafeau_delete($link) {
+  if(!file_exists(VAR_LINKS . $link))
+    return;
+
+  $content = file(VAR_LINKS . $link);
+  $md5 = trim($content[5]);
+  unlink(VAR_LINKS . $link);
+
+  $counter = 1;
+  if (file_exists(VAR_FILES . $md5 . '_count')) {
+    $content = file(VAR_FILES . $md5 . '_count');
+    $counter = trim($content[0]);
+  }
+  $counter--;
+
+  if ($counter >= 1) {
+    $handle = fopen(VAR_FILES . $md5 . '_count', 'w');
+    fwrite($handle, $counter);
+    fclose($handle);
+  }
+
+  if ($counter == 0 && file_exists(VAR_FILES. $md5)) {
+    unlink (VAR_FILES . $md5);
+    unlink (VAR_FILES . $md5 . '_count');
+  }
+}
+
 /**
  * handles an uploaded file
  * @param $file the file struct given by $_FILE[]
   fclose($handle);
   $md5_link = md5_file($link_tmp_name);
   if(!rename($link_tmp_name, VAR_LINKS . $md5_link)) {
-    if ($counter > 1) {
-      $counter--;
+    unlink($link_tmp_name);
+    $counter--;
+    if ($counter >= 1) {
       $handle = fopen(VAR_FILES . $md5 . '_count', 'w');
       fwrite($handle, $counter);
       fclose($handle);
     }
     else {
-      unlink($link_tmp_name);
-      unlink(VAR_FILE . $md5 . '_count');
-      unlink(VAR_FILE . $md5);
+      unlink(VAR_FILES . $md5 . '_count');
+      unlink(VAR_FILES . $md5);
     }
     return(array(
       'error' => array(
 
 
 define('VAR_FILES', $cfg['var_root'] . 'files/');
 define('VAR_LINKS', $cfg['var_root'] . 'links/');
-define('VAR_TRASH', $cfg['var_root'] . 'trash/');
 
 // i18n