Correctly handle thread related events
Various fixes