UI stuff.
Added 'ref' where missing to a bunch of foreach loops.