added stats files and enclosed function