Support references.

Rework the wrapper considerably so that references are supported.
Add unit tests.