 +    HOST=$4
 +
 +    if [ "x$HOST" != "x" ]; then
-+        HOST="ssh $HOST"
++        SSH_CMD="ssh $HOST"
 +    fi
 +
-+    $HOST curl -k -o $ARCHIVE $URL
++    $SSH_CMD curl -k -o $ARCHIVE $URL
 +
-+    extract_tar $ARCHIVE $FILENAME
++    extract_tar $ARCHIVE $FILENAME $HOST
 +
-+    rm -rf $ARCHIVE
++    $SSH_CMD rm -rf $ARCHIVE
 +}
 +
 +function file_ends_with

diff --git a/src/tm_mad/tm_common.sh b/src/tm_mad/tm_common.sh
index 0f589fe..1e98122 100644
--- a/src/tm_mad/tm_common.sh
+++ b/src/tm_mad/tm_common.sh
@@ -145,3 +145,56 @@ function timeout_exec_and_log
         exit $CMD_CODE
     fi
 }
+ 
+function download_extract
+{
+    URL=$1

+{
+    ARCHIVE=$1
+    OUTPUT=$2
+    HOST=$3
+
+    if [ $(file_ends_with $ARCHIVE '.tar.gz') = 0 ]; then
+        FORMAT=z
+    elif [ $(file_ends_with $ARCHIVE '.tar.bz2') = 0 ]; then
+        FORMAT=j
+    elif [ $(file_ends_with $ARCHIVE '.tar.xz') = 0 ]; then
+        FORMAT=J
+    fi
+    
+    TAR_CMD="tar -x${FORMAT}f $ARCHIVE"
+
+    if [ "x$HOST" != "x" ]; then
+       SSH_CMD="ssh $HOST"
+    fi
+
+    if [ "x$OUTPUT" != "x" ]; then
+        if [ "x$HOST" != "x" ]; then
+            $SSH_CMD "$TAR_CMD -O > $OUTPUT"
+        else
+            $TAR_CMD -O > $OUTPUT
+        fi
+    else
+        $SSH_CMD $TAR_CMD 
+    fi
+}
+
