Moved away the database code

Moved the code that handles the database into database.js