Beginnings of an API (v2)

Also some bugfixes...