fix incorrect loop count