add counter and length check