Move $ua outside foreach-loop.