cleaned with JSHint
moved character escaping to a function