Rewrote foreach to handle vector outputs with templates