Moved stats function into common code.