re-arranging the database connection code to be a bit cleaner