refactored some of the database code