Fixing loop error