Handle thread issues and message passing