Improving stats function and adding a new query