updating foreach code to be more general, easier to use; other operators to follow