slightly improved memory management and thread synchronization