Bug  in for loop fixed