Please comment in the code what this is doing. I don't quite understand and cannot tell if there are problems with escaping, null values/empty lists, etc.