Fix for #1521
Fix for #1521

The merge from #1521 fixed the invalid variable error
but it fixed in in a way that introduced another error:
"dictionary changed size during iteration"
This correctly applies the fix.

alexanderlz/redash,getredash/redash,hudl/redash,crowdworks/redash,amino-data/redash,moritz9/redash,chriszs/redash,denisov-vlad/redash,rockwotj/redash,moritz9/redash,denisov-vlad/redash,EverlyWell/redash,denisov-vlad/redash,44px/redash,M32Media/redash,useabode/redash,getredash/redash,moritz9/redash,vishesh92/redash,hudl/redash,crowdworks/redash,amino-data/redash,EverlyWell/redash,alexanderlz/redash,vishesh92/redash,denisov-vlad/redash,M32Media/redash,44px/redash,hudl/redash,EverlyWell/redash,moritz9/redash,getredash/redash,imsally/redash,imsally/redash,useabode/redash,imsally/redash,M32Media/redash,denisov-vlad/redash,amino-data/redash,chriszs/redash,vishesh92/redash,alexanderlz/redash,getredash/redash,rockwotj/redash,44px/redash,crowdworks/redash,hudl/redash,rockwotj/redash,EverlyWell/redash,rockwotj/redash,44px/redash,getredash/redash,chriszs/redash,crowdworks/redash,vishesh92/redash,useabode/redash,alexanderlz/redash,amino-data/redash,useabode/redash,imsally/redash,chriszs/redash,M32Media/redash