move some code out thread