             encodeString("");
         }
 
-        context.oRef.table[object] = context.oRef.ix ++;
-    }
-
-
-    private static function encodeByteArray (context :Context, bytes :ByteArray) :void
-    {
-        if (context.oRef.table[object] !== undefined) {
-            encodeInteger(context.oRef.table[object] << 1);
+        _ctx.oRef.table[object] = _ctx.oRef.ix ++;
+    }
+
+
+    private static function encodeByteArray (bytes :ByteArray) :void
+    {
+        if (_ctx.oRef.table[bytes] !== undefined) {
+            encodeInteger(_ctx.oRef.table[bytes] << 1);
             return;
         }
 
         // a byte array consists of a 1 bit, followed by the byte count
         encodeInteger(1 | (bytes.length << 1));
         // and then just... all the bytes
-        context.bytes.writeBytes(bytes);
-
-        context.oRef.table[object] = context.oRef.ix ++;
-    }
-}
-}
+        _ctx.bytes.writeBytes(bytes);
+
+        _ctx.oRef.table[bytes] = _ctx.oRef.ix ++;
+    }
+
+    private static var _ctx :Context;
+}
+}
+
+import flash.utils.ByteArray;
+import flash.utils.Dictionary;
 
 class Context
 {

{
    public static function encode (bytes :ByteArray, value :*) :void
    {
        encodeValue(new Context(bytes), value);
    }

    private static function encodeValue (context :Context, value :*) :void
    {
        if (value === undefined) {
            context.bytes.writeByte(0x00);

        } else if (value === null) {
            context.bytes.writeByte(0x01);

        } else if (value === false) {
            context.bytes.writeByte(0x02);

        } else if (value === true) {
            context.bytes.writeByte(0x03);            

        } else if (value is int || value is uint || value is Number) {
            // figure out if this number could be represented as an AMF3 int
            if (value is int) {
                // signed integers must lie within -2^28 and 2^28-1
                doublify = (value < -(1 << 28)) || (value >= (1 << 28));

        }
        // and values
        for (ii = 0; ii < dynVars.length; ii ++) {
            encodeValue(object[dynVars[ii]]);
        }

        // if there were any dynamic variables (which we signaled with a bit above),
