Refactored database connection code.