Trying to optimize memory allocation