Reorganize db functions.