Improve and test the error handling of the #REFS macro