refactor stats and logging