Added database logic, will perhaps move this to models later