Implement thread handling