Add lots of tests for pack/unpack code, fix bugs found