Fix ref expansion bugs