Corrected code for scenarios where the parent was not set due to some error.