Changed the API slightly as it evolves.
Untested implementation.