Fix memory management