refactored db code