BF/TST: fix internals of jailreader and add test cases

and a few bug fixes discovered along the way.