improved logic of count