Casting to array in the foreach would simplify this.

BTWThis looks like it should work. @cash where do you see this failing?