fixing issue with callback inside loop