Refactored the database functionality into a class. Fixed a problem with multiple db sessions being instantiated.