Use ArrayIterator instead of Iterator and ensure variables are available after refactor