Refactored database access