Fixed thread exiting, added support for variable thread count.