safer implementation of dual array iteration