Added proper memory management functions.