prepare the command outside the loop
put this code in a separate function