count_ngrams totally reworked and optimized