Improved character escaping