Changed while loop, so that it won't prevent the program to exit.