added counter logic