move db gen outside of loop