Fix occasional crashes when invalid data is received.

For #2