Fixed some multithreading related memory problems/segfaults.