move db stuff to its own function