Improve reference handling #583