refactor db functions