Reorganize DB code