Threads optimization. Code clean-up.