Add countBy function