Fixed bug where for-loop variable name was reused.