Made some refactoring and added stats collecting.