added string escaping and error handling
merged