refactor db functionality