fix some bugs, add stats logging