moving DB access around; refatoring data analysis code