sorted out counting code