loop error handling added