changes from using $j variabble to using count() in for loop