refactor db code