Improve input sanitation and encoding

This is a work in progress to address #416. Can people please comment with any tests they know need address (preferable in the form of a input and expected) and I'll get them passing.

Thank you all!! (cc @markijbema @totallymike @swanson @dodecaphonic @rubysolo)