Beginnings of an API

Also some bugfixes...