Make better thread handling