Several optimizations to memory allocation