Implement proper shutdown for all threads