fix nested loop