refactored database access