added permutation and fixed counting