adding while loop for terminate function