substitution with refs in own function + name collision fix
now generate index distribution for foreach loops by calling a function