fixed memory leaks in NLEQ, also fixed threading issue