added a try catch round the main loop