added while loop version