Fixed threading bug that could cause segfaults.