Fiddled a bit with the code for transforming references