Reorganize main initialization code to new thread model