Optimizing memory usage