Rewrite memory allocation