add stats and refactor code