fix scope issue in generateStats loop