Some thread support. Still needs tweeking.