Avoid crashing on some invalid input

Fixes #9