Fixed foreach bug

The loop over the elements was clobbering all the values.  I switched this to a for loop so it now works with typed arrays.