Fixes #3, #15, potential integer overflow