Check encoding before applying htmlentities

Fix for #48