refactor thread creation code

to have it in a single location hence making change to it easier.