Handle error array and cleaned up the loop