Fixed some bugs in ref resolution