fixed some escaping of characters (still need tests), and added some tests