Add memory management code