Threads fix

Many fixes and enhancements