Correct header length checking logic. Improve exception message.

Closes #335.