Refactored database layer