updated counting code