Major cleanup of db code.