Fixed the inconsistent count issue and some crashes