Miscellaneous fixes.

- Pay close attention to the signedness of integers;
- Sanitise and check arguments more strictly;
- Use uint8_t where applicable;
- Rework a few odd constructions;

No functional change.