Fixed loop error on reference to var i