Adding exception handing to shut down cleanly