Several tweaks to memory allocation