added thread wrapping code for collapsing