refactoring database connection code