improved error handling in thread creation