added a try catch if the internet drops and split the while loop into functions