Updated code to exit gracefully.