fix making function in loop