Fix memory handling