Improved encodeMap