Fixed scope issue when using for loops; switched to .each