Added handing of distinct and first pass on hadling of count