Various fixes on memory allocation.