Fix ref handling