Adds stats module, some tests, rafactorings