Some refactoring of database code