refactored to make db access simpler