Moving stats scripts to a function