Refactor some methods, add test for string escapes

r? @bjz