diff --git a/tools/osxcross-macports b/tools/osxcross-macports
index f008a8d..23dd68d 100755
--- a/tools/osxcross-macports
+++ b/tools/osxcross-macports
@@ -181,7 +181,7 @@ selectMirror()
 }

 function download()
-{
+(
   local uri=$1
   local filename

@@ -191,6 +191,18 @@ function download()
     filename=$(basename $1)
   fi

+  # Remove file if any error occurs during download
+  trap 'rm -f "$filename"' ERR
+
+  # Return if file already exists in cache
+  case "$filename" in
+    "$CACHE"*)
+      if [ -e "$filename" ]; then
+        return
+      fi
+      ;;
+  esac
+
   if command -v curl &>/dev/null; then
     ## cURL ##
     local curl_opts="-L -C - "
@@ -220,7 +232,7 @@ function download()
     echo "Required dependency 'curl or wget' not installed" 1>&2
     exit 1
   fi
-}
+)

 getFileStdout()
 {
