Moved stats db function into db