Cleaner exiting

Make main thread wait until all threads are closed before completing shutdown.