Be more careful about bounds-checking. Fixes #45.