Fix insertion of references

as suggested by @lynnard in issue #116.