corrected looping over arrays