Refactoring database connection