Added more memory management (hopefully).