Refactor and change stats logging
Change recurson to iteration and handle mor exceptions