Remove undefined behavior in offset computation

Closes #156