Moving stats calculations to db