refactored a db setup function, needs unit testing.