Fix memory management issue