fixed loop error / merged functions