-    while [ $(grepbackups $2 | wc -l) -gt $ARCHIVE_KEEPNBACKUPS ]; do
-       oldestbackup=$(grepbackups $2 | head -1)
-       oldestbackuptstamp=$(echo $oldestbackup | cut -d'-' -f1)
+    while [ $(grepbackups | wc -l) -gt $ARCHIVE_KEEPNBACKUPS ]; do
+        oldestbackup=$(grepbackups | head -1)
+        oldestbackuptstamp=$(echo $oldestbackup | cut -d'-' -f1)
+
+        if [ $oldestbackuptstamp -lt $(( $(date +%s) - $ARCHIVE_KEEPNDAYS*60*60*24 )) ]; then
+            scottyinfo "Removing old backup $oldestbackup."
+            ssh255 $REMOTE_HOST rm -r "$REMOTE_DIR/$oldestbackup"
+        else
+            break
+        fi
+    done
+}
+
+function deleteLock {
+    if ! rmdir /var/lock/$(basename $0); then
+        scottyerror "Could not delete lockfile /tmp/$(basename $0).lock!"
+    fi
+}
+
+function cleanup_abort {
+    scottyerror "Caught exit signal! Cleaning up."
+
+    cleanup ABORT
+
+    if [ $(jobs -p) ]; then
+        scottyerror "TERMinating remaining child processes."
+        kill $(jobs -p)
+    fi
+
+    deleteLock
+
+    exit
+}
+
+function cleanup {
+    scottyinfo "No cleanup function was defined."
+}
+
+function prepare {
+    scottyinfo "No prepare function was defined."
+}
+
+function cleanup_normal {
+    cleanup
+    deleteLock
+}
+
+function printhelp {
+    cat <<EOF
+Usage: $(basename $0) [OPTION]...