Fix pointer arithmetic undefined behavior.

Reported by Denis Denisov.
Fix more pointer arithmetic undefined behavior.

Reported by Guilherme Gonçalves.

This resolves #166.