Numerous fixes around shutdown and threading.