changing 'countBy' method to avoid problems with the 'same' string with different typing
adding some refactors and a better code documentation