Refactor some more db code, move to model