Added support for threads + some changes for legibility, factorization, code standarization, etc