diff --git a/.gitignore b/.gitignore
index 611dd26..b53de30 100644
--- a/.gitignore
+++ b/.gitignore
@@ -35,3 +35,6 @@ bower_components
 
 # Temp files
 *.swp
+
+#Db Files
+url.db
diff --git a/app.js b/app.js
index e7b0579..50ddf27 100644
--- a/app.js
+++ b/app.js
@@ -2,7 +2,9 @@ const express = require('express');
 const bodyParser = require('body-parser');
 const cryptohat = require('cryptohat');
 const cron = require('cron');
-const Ddos = require('ddos')
+const Ddos = require('ddos');
+var sqlite3 = require('sqlite3').verbose();
+var fs = require("fs");
 
 const app = express();
 
@@ -18,24 +20,49 @@ const HASH_LENGTH = 5;
 const CLEANSE_FREQ = '0 0 4 * * *';
 const EXPIRY_TIME = 48;
 var maps = {};
+var file = "url.db";
+var exists = fs.existsSync(file);
+var db = new sqlite3.Database(file);
 
 var cleanse = new cron.CronJob(CLEANSE_FREQ, function() {
     var current_date = new Date();
-    for(var key in maps) {
-        if(maps[key]['expiry'] < current_date) {
-            delete maps[key];
-        }
-    }
+    db.serialize(function() {
+        db.each("SELECT rowid AS id, expiry FROM user_info", function(err, row) {
+           if (new Date(row.expiry) < current_date) {
+                db.run("DELETE FROM user_info WHERE rowid=(?)", row.id, function(error) {
+                if (error){
+                    console.log(error);
+                } else {
+                    console.log("Deletion Successful");
+                }
+            });
+           }
+        });
+    });
 });
 
+
 app.get('/*', function(req, res, next) {
-    if(req.url.substr(1) in maps) {
-        res.redirect(maps[req.url.substr(1)]['url']);
-    } else {
-        next();
-    }
+    db.serialize(function() {
+        db.each("SELECT url FROM user_info WHERE shrunk = '" + req.url.substr(1) + "'", function(err, row) {
+           if (err) {
+                console.log(err);
+           } else {
+                res.redirect(row.url);
+           }
+        }, function(error, rows) { // oncomplete functions
+            if (error) {
+                console.log(error);
+            } else {
+                if (rows === 0) { // the case where the url doesn't exist in db
+                    next();
+                }
+            }
+        });
+    });
 });
 
+
 app.get('/', function (req, res) {
     res.sendfile(__dirname + '/public/index.html');
 });
@@ -62,11 +89,7 @@ app.post('/add', function(req, res) {
     }
     expiry = new Date();
     expiry.setHours(expiry.getHours() + EXPIRY_TIME);
-    maps[shrunk] = {
-        'url' : url.trim,
-        'expiry' : expiry,
-    };
-
+    addUrlToDb(shrunk, (url.trim()).toString(), expiry.toString());
     // Return the new URL
     res.writeHead(200, {
         "Content-Type": "text/plain"
@@ -77,3 +100,29 @@ app.post('/add', function(req, res) {
 app.listen(port, function () {
     console.log('z-9 listening on port ' + port + '.');
 });
+
+//initalizes the database i.e creates the db.url file if it doens't already exist
+function initDb() {
+    if (!exists) {
+        console.log("Creating DB file.");
+        fs.openSync(file, "w");
+        db.serialize(function() {
+            db.run("CREATE TABLE if not exists user_info (shrunk TEXT UNIQUE, url TEXT, expiry TEXT)");
+        });
+    }
+}
+
+//adds new urls to the database
+function addUrlToDb(shrunk, url, expiry) {
+    db.serialize(function() {
+        var stmt = db.prepare("INSERT INTO user_info VALUES (?,?,?)");
+        stmt.run(shrunk, url, expiry);
+        stmt.finalize();
+        db.each("SELECT rowid AS id, shrunk, url, expiry FROM user_info", function(err, row) {
+           console.log(row.id + ": " + row.shrunk + "    " +  row.url + "    "  + row.expiry);
+        });
+    });
+}
+
+initDb();
+cleanse.start();
diff --git a/package.json b/package.json
index 7a90084..998408b 100644
--- a/package.json
+++ b/package.json
@@ -23,6 +23,7 @@
     "cryptohat": "^1.0.1",
     "ddos": "^0.1.11",
     "express": "^4.13.4",
-    "pm2": "^1.1.3"
+    "pm2": "^1.1.3",
+    "sqlite3": "^3.1.8"
   }
 }