factor out some database code