Corrected one minor but crusial mistake that lead to infinite iteration