diff --git a/urlicon.module b/urlicon.module
index c13705c..3d76eb8 100644
--- a/urlicon.module
+++ b/urlicon.module
@@ -153,41 +153,44 @@ function _urlicon_format_favicon($match) {
   }catch(Exception $e) {
     watchdog('urlicon', "Error @error", array('@error' => $e->getMessage()));
   }
-
+  
   //check if favicon exists locally
   if ($url['host'] AND !file_exists($dir .'/'. $domain .'.ico')) {
   
-    //check for favicon in metatags
-    $data = drupal_http_request(check_url($match[1]));
-    
-    if (preg_match('/<link[^>]+rel="(?:shortcut )?icon"[^>]+?href="([^"]+?)"/si', $data->data, $icons)) {
+    // Check the remote content-type before downloading the content
+    $data = drupal_http_request(check_url($match[1]), array(), 'HEAD');
+    if(is_object($data) && (!empty($data->headers['Content-Type'])) && preg_match('/^text\/html/i', $data->headers['Content-Type'])) {
+      //check for favicon in metatags
+      $data = drupal_http_request(check_url($match[1]));
       
-      if (strpos($icons[1], '://')) {
-        // absolute path
-        $data = drupal_http_request(check_url($icons[1]));
-      }
-      else if (substr($icons[1], 0, 3) == '../') {
-        // relative path
-        $path = '';
-        $elements = explode('/', $url['path']);
-        $i = 0;
-        while (!strpos($elements[$i], '.') AND $i <= count($elements)) {
-          $path .= $elements[$i] .'/';
-          $i++;
-        }
+      if (preg_match('/<link[^>]+rel="(?:shortcut )?icon"[^>]+?href="([^"]+?)"/si', $data->data, $icons)) {
         
-        $data = drupal_http_request(check_url($url['scheme'] .'://'. $url['host'] . $path . $icons[1]));
-      }
-      else if (substr($icons[1], 0, 1) == '/') {
-        // relative path
-        $data = drupal_http_request(check_url($url['scheme'] .'://'. $url['host'] . $icons[1]));
-      }
-      else {
-        // get favicon from webroot
-        $data = drupal_http_request(check_url('http://'. $url['host'] .'/favicon.ico'));
-        watchdog('urlicon', t('Could not find favicon for URL %url with shortcut url %shortcut, trying webroot.', array('%url' => $match[1], '%shortcut' => $icons[1])), WATCHDOG_ERROR);
-      }
-      
+        if (strpos($icons[1], '://')) {
+          // absolute path
+          $data = drupal_http_request(check_url($icons[1]));
+        }
+        else if (substr($icons[1], 0, 3) == '../') {
+          // relative path
+          $path = '';
+          $elements = explode('/', $url['path']);
+          $i = 0;
+          while (!strpos($elements[$i], '.') AND $i <= count($elements)) {
+            $path .= $elements[$i] .'/';
+            $i++;
+          }
+          
+          $data = drupal_http_request(check_url($url['scheme'] .'://'. $url['host'] . $path . $icons[1]));
+        }
+        else if (substr($icons[1], 0, 1) == '/') {
+          // relative path
+          $data = drupal_http_request(check_url($url['scheme'] .'://'. $url['host'] . $icons[1]));
+        }
+        else {
+          // get favicon from webroot
+          $data = drupal_http_request(check_url('http://'. $url['host'] .'/favicon.ico'));
+          watchdog('urlicon', t('Could not find favicon for URL %url with shortcut url %shortcut, trying webroot.', array('%url' => $match[1], '%shortcut' => $icons[1])), WATCHDOG_ERROR);
+        }
+      }      
     }
     else {
       // get favicon from webroot

