refactor; tests; some escaping fixes