First bits, mostly working though not complete.

First bits of the library work, the API is probably set
mostly now, but some parts are missing. For our current
use-case, it's complete enough, so I'll continue later.