corrected the alghoritm for j value