refactored to use CopyOnRead template for Array and Map loops