 			return (JUMP_LOOKUP[method] || LOOP_LOOKUP[method])
 		}
 		
-		private function optimize(compiledObject:ICompiledObject):ICompiledObject
+		/**
+		 * Call this to move all var declarations at the beginning of the code and perform optimizations on the compiled objects.
+		 * @param compiledObject An ICompiledObject to finalize.
+		 * @return A finialized/optimized version of compiledObject.
+		 */
+		private function finalize(compiledObject:ICompiledObject):ICompiledObject
+		{
+			var varLookup:Object = {};
+			
+			compiledObject = _finalize(compiledObject, varLookup);
+			
+			var names:Array = [];
+			for (var name:String in varLookup)
+				names.push(name);
+			
+			if (names.length > 0)
+			{
+				// there is at least one var declaration, so we need to include it at the beginning.
+				var varDeclarations:CompiledFunctionCall = compileOperator(ST_VAR, [new CompiledConstant(null, names)]);
+				var call:CompiledFunctionCall = compiledObject as CompiledFunctionCall
+				if (call && call.evaluatedMethod == operators[';'])
+				{
+					call.compiledParams.unshift(varDeclarations);
+					call.evaluateConstants();
+				}
+				else
+					compiledObject = compileOperator(';', [varDeclarations, compiledObject]);
+			}
+			return compiledObject;
+		}
+		/**
+		 * @private helper function
+		 */
+		private function _finalize(compiledObject:ICompiledObject, varLookup:Object):ICompiledObject
 		{
 			if (compiledObject is CompiledConstant)
 				return compiledObject;
 			
 			var call:CompiledFunctionCall = compiledObject as CompiledFunctionCall;
-			
-			call.compiledMethod = optimize(call.compiledMethod);
+			call.compiledMethod = _finalize(call.compiledMethod, varLookup);
+			if (!call.compiledMethod)
+				throw new Error("Misplaced variable declaration");
 			var params:Array = call.compiledParams;
 			if (params)
+			{
 				for (var i:int = 0; i < params.length; i++)
-					params[i] = optimize(params[i]);
+				{
+					params[i] = _finalize(params[i], varLookup);
+					if (params[i] == null) // variable declaration eliminated?
+						params.splice(i--, 1);
+				}
+			}
 			call.evaluateConstants();
 			
 			var method:Object = call.evaluatedMethod;
+			
+			// remove var declarations from their current locations
+			if (method == operators[ST_VAR])
+			{
+				for each (var name:String in call.evaluatedParams[0])
+					varLookup[name] = true;
+				return null;
+			}
+			
 			if (method == operators[';'] || method == operators[','] || method == operators['('])
 			{
 				if (params.length == 0)

				
				if (leftBracket == '{')
				{
					// It's ok if it creates an extra {} wrapper because optimize() will take care of that.
					// It's important to remember that the brackets existed for statement processing.
					tokens.splice(open, 2, compileOperator(';', [compileTokens(subArray, true)]));
					

		 * Used in conjunction with _validStatementPatterns.
		 */
		private static const PN_EXPR:String = 'EXPR';
		
		/**
		 * may only contain one expression, optionally enclosed in (), no statements.
		 * Used in conjunction with _validStatementPatterns.
		 */
		private static const PN_VARS:String = 'VARS';
		
		/**
		 * longer patterns appear earlier so they will match before shorter patterns when checked in order
