exit properly and shutdown all threads