Refactored database access, moved logic out of db.py into controller.py