Added more memory management code