diff --git a/chart.module b/chart.module
index 9e92752..fd20930 100644
--- a/chart.module
+++ b/chart.module
@@ -175,7 +175,14 @@ function chart_url($chart) {
     }
     $count++;
 
-    return url('//' . $uri, array('query' => $data, 'external' => TRUE));
+    if ($_SERVER['HTTPS'] == "on") {
+      $protocol = "https:";
+    }
+    else {
+      $protocol = 'http:';
+    }
+
+    return url($protocol . '//' . $uri, array('query' => $data, 'external' => TRUE));
   }
   return FALSE;
 }
@@ -209,28 +216,31 @@ function chart_copy($chart, $name = NULL, $dest = 'charts', $replace = FILE_EXIS
     return FALSE;
   }
 
-  if ($dest = file_create_path($dest)) {
-    if (file_prepare_directory($dest, FILE_CREATE_DIRECTORY)) {
-      // Defaults
-      $name = is_null($name) ? $chart['#chart_id'] : $name;
-
-      // Generate temp image
-      $tempname = tempnam(file_directory_temp(), 'tmp_');
-      $filename = file_create_path($dest) . '/' . $name . '.png';
-      $png      = imagecreatefrompng(chart_url($chart));
-      $fh       = fopen($tempname, 'w+');
-      imagepng($png, $tempname);
-
-      // Copy temp image to new location
-      if (!file_copy($tempname, $filename, $replace)) {
-        _chart_error(t('Failed to copy chart image.'));
-        return FALSE;
-      }
+  if (file_prepare_directory($dest, FILE_CREATE_DIRECTORY)) {
+    // Defaults
+    $name = is_null($name) ? $chart['#chart_id'] : $name;
 
-      // Remove temp image
-      fclose($fh);
-      return $filename;
+    // Generate temp image
+    $tempname = tempnam(file_directory_temp(), 'tmp_');
+    if (!file_prepare_directory($dest)) {
+      _chart_error(t('Failed to prepare destination directory.'));
+      return FALSE;      
+    }
+    $filename = $dest . DIRECTORY_SEPARATOR . $name . '.png';
+    $png      = imagecreatefrompng(chart_url($chart));
+    $fh       = fopen($tempname, 'w+');
+    imagepng($png, $tempname);
+     
+    // Copy temp image to new location
+    //if (!file_copy($tempname, $filename, $replace)) {
+    if (!copy($tempname, $filename)) {
+      _chart_error(t('Failed to copy chart image.'));
+      return FALSE;
     }
+
+    // Remove temp image
+    fclose($fh);
+    return $filename;      
   }
 
   return FALSE;
