refactored db code, not yet working