Add tests for Codec, and fix arithmetic errors