tojson -> to_json, added attr_if, minor bugfixes