Added bounds-checking for prevCount and nextCount.