Please extract this loop into a function. You are using twice the same code.