Wrote a testcase and fixed a bug.
The encoding is still not correct though...
Idea for a prefix tree encoder.