refactored db_wrap