corrected code to iterate over all rows