 
 		# Takes a "raw" context object and orders it into the internal _context
 		# storage.  Also builds constructors and prototypes for the context.
-		_set_context = (context, protos) ->
+		_set_context = (context, protos) =>
 			# Save the attributes
-			_.extend(ec._context.attributes, context.attributes)
-
-			# Prepare each entity
-			_.each(context.entities, (definition, name) ->
+			_.extend ec._context.attributes, context.attributes
+
+			# Prepare each entity. Uses _.each to put (definition, name) in a closure.
+			_.each context.entities, (definition, name) ->
 				# Keep the definition locally, modifying it directly with the ctor and prototypes.
 				ec._context.entities[name] = definition
 				# Ready the instances bucket

				# If the entity key is specified in obj, check the local storage.
				demi = {_type : type}
				demi[def.key] = obj[def.key]
				instance = this.find(demi)
				if (instance.length > 0) 
					# Local instance, extend it with the new obj and return local.
					instance = instance[0]
					_.extend(instance._fields, r._fields)
					return instance
			this._instances[type][r.id()] = r
			this._new.push(r)
			return r

		# Expand and intern a transaction.
		expand: (transaction, action) ->
			self = this
			entities = transaction.entities
			action = action || "persisted"

			ret = []

			if (entities)
				_.each(entities, (entity) ->
					e = self.build(entity._type, entity)
					e = self.intern(e, true)
					#Make the entity not new...
					_.trigger(e, action)
					ret.push(e)
				)

			transaction.entities = ret
			return ret

					data = _.isString(data) && JSON.parse(data) || data
					_set_context(data, protos)
			)
		@

	# #### Entity Array helper
	pushResult = (entity) ->
		type = entity._type()
		if (!@[type]) then @[type] = []
		@[type].push(entity)

	# #### Runtime Prototype
	root.JEFRi.Runtime.prototype = _.extend {}, JEFRi.Runtime.prototype,
		# Reset the runtime's data, maintains context definitions.
		clear: () ->
			@_modified = {}
			@_new = []
			@_instances = {}
			return @

		# Get the definition of an entity type.
		definition: (name) ->
			name = (typeof name is "string") && name || name._type()

			return @_context.entities[name]

		# Find the relationship back to this entity, if it exists
		back_rel: (type, field, relationship) ->
			ec = @
			def = ec.definition(relationship.to.type)
			back = null
			for srel_name, rel of def.relationships
				if (rel.to.type is type && srel_name isnt field)
					#Found it
					back = rel
					back.name = srel_name
			return back

		# Add the methods in the extend prototype to the prototype of type specified
		# affecting _ALL_ instances, both current and future, of type.
		extend: (type, extend) ->
			if (@_context.entities[type])
				_.extend @_context.entities[type].Constructor.prototype, extend.prototype
		

		# Return the canonical memory reference of the entity.
		intern: (entity, updateOnIntern) ->
			updateOnIntern = !!updateOnIntern || @settings.updateOnIntern

			if (entity.length && ! entity._type)
				#Array-like
				q = entity.length
				for i in [0..q]
					entity[i] = @intern(entity[i], updateOnIntern)
				return entity

			ret
			if (updateOnIntern)
				#Merge the given entity into the stored entity.
				ret = @_instances[entity._type()][entity.id()] || entity
				_.extend ret._fields, entity._fields
			else
				#Take the stored one if possible, otherwise use the given entity.
				ret = @_instances[entity._type()][entity.id()] || entity

			#Update the saved entity
			@_instances[entity._type()][entity.id()] = ret
			return ret

		# Return a new instance of an object described in the context.
		build: (type, obj) ->
			def = @definition(type)
			obj = obj || {}
			# We are going to build the new entity first, then, if there is a local
			# instance, we will extend the local instance with the new instance.
