added new keyExists check to avoid created unecessary undefined keys