Replace for's and some while's with foreach, removed inline vars.