Finished enormous refactor around loops so that C#-style inner-scopes are preserved