Memory optimizations

Various memory optimizations.