First merge of new stats into develop

This is a huge chunk of code, it's incomplete, and there are TODOs.