New function for memory handling