Fixed a nasty bug with references