Code runs for test cases.  Problems with while() loop