better detection and handling of malformed Unicode, thanks to @jallmantalbot. fixes #26.