SG_REF has to be done for both the cases. I would put it after the endif.