Fixes parsing of invalid UTF-8 characters

Followed advice from http://stackoverflow.com/a/8873922