Add thread manager, rewrite large chunks of threads and reorder stacks