changed back to using $j variable