fixed incorrect usage of counter, fixed incorrect usage of var