Refactored all database code into its own library.