added code to collect stats