Fixed calculating references.
Tests.