added version using while loop