better htmlentity handling, research prompted by @harrisj's update