Major threading + memory fixes.