statistics: port a lot stuff, compiling but not working yet