Avoid integer overflow in distance calculation

Fixes #3, without using a poor distance calculation.