--[[Engine]]



	local fFD = _Tune.FinalDrive
	local fFDr = fFD*30/math.pi
	local wDRatio = wDia*math.pi/60
	local cfWRot = CFrame.Angles(math.pi/2,-math.pi/2,0)
	local cfYRot = CFrame.Angles(0,math.pi,0)
	
	--Electric Only Setup
	
	if not _Tune.Engine and _Tune.Electric then 
		_Tune.Redline = _Tune.E_Redline 
		_Tune.PeakRPM = _Tune.E_Trans2 
		_Tune.Turbochargers = 0
		_Tune.Superchargers = 0
		_Tune.Clutch = false 
		_Tune.IdleRPM = 0 
		_Tune.AutoShiftType = "DCT" 
		_Tune.ShiftUpTime = 0.1 
		_Tune.ShiftDnTime = 0.1 
	end
	
	--Aspiration Setup
	
	_TCount = _Tune.Turbochargers
	_TPsi = _Tune.T_Boost*_Tune.Turbochargers
	
	_SCount = _Tune.Superchargers
	_SPsi = _Tune.S_Boost*_Tune.Superchargers
	
	--Engine Curve
	
	local HP_M = _Tune.Horsepower
	local HP_T = HP_M*(((_TPsi*(_Tune.T_Efficiency/10))/7.5)/2)
	local HP_S = HP_M*(((_SPsi*(_Tune.S_Efficiency/10))/7.5)/2)
	local PK_H, PK_T = _Tune.PeakRPM, _Tune.MT_PeakRPM
	local MC_H, MC_T = _Tune.MH_CurveMult, _Tune.MT_CurveMult
	local TC_H, TC_T = _Tune.TH_CurveMult, _Tune.TT_CurveMult
	local SC_H, SC_T = _Tune.SH_CurveMult, _Tune.ST_CurveMult
	local MP_H, MP_T = _Tune.MH_PeakSharp, _Tune.MT_PeakSharp
	local TP_H, TP_T = _Tune.TH_PeakSharp, _Tune.TT_PeakSharp
	local SP_H, SP_T = _Tune.SH_PeakSharp, _Tune.ST_PeakSharp
	local V_MT = {HP_M,PK_H,MC_H,MP_H,PK_T,MC_T,MP_T}
	local V_TC = {HP_T,PK_H,TC_H,TP_H,PK_T,TC_T,TP_T}
	local V_SC = {HP_S,PK_H,SC_H,SP_H,PK_T,SC_T,SP_T}
	
	function getCurve(RPM,HP,Peak,CurveMult,Sharpness)
		RPM=RPM/1000
		HP=HP/100
		Peak=Peak/1000
		return ((-(RPM-Peak)^2)*math.min(HP/(Peak^2),CurveMult^(Peak/HP))+HP)*(RPM-((RPM^Sharpness)/(Sharpness*Peak^(Sharpness-1))))
	end
	
	function getPeak(V)
		local HP,Peak_H,CurveMult_H,Sharpness_H,Peak_T,CurveMult_T,Sharpness_T=V[1]/100,V[2]/1000,V[3],V[4],V[5]/1000,V[6],V[7]
		local hp=((-(Peak_H-Peak_H)^2)*math.min(HP/(Peak_H^2),CurveMult_H^(Peak_H/HP))+HP)*(Peak_H-((Peak_H^Sharpness_H)/(Sharpness_H*Peak_H^(Sharpness_H-1))))
		local tq=((-(Peak_T-Peak_T)^2)*math.min(HP/(Peak_T^2),CurveMult_T^(Peak_T/HP))+HP)*(Peak_T-((Peak_T^Sharpness_T)/(Sharpness_T*Peak_T^(Sharpness_T-1))))
		return hp, tq
	end
	
	local PeakHP_N, PeakTQ_N = getPeak(V_MT)
	local PeakHP_T, PeakTQ_T = getPeak(V_TC)
	local PeakHP_S, PeakTQ_S = getPeak(V_SC)
	
	--Plot Current Horsepower
	function EngineCurve(x,gear,V,eq,peakhp,peaktq)
		local hp,peak_h,cm_h,sharp_h,peak_t,cm_t,sharp_t=V[1],V[2],V[3],V[4],V[5],V[6],V[7]
		local hp_r=(math.max(getCurve(x,hp,peak_h,cm_h,sharp_h)/(peakhp/hp),0))
		local tq_r=(math.max(getCurve(x,hp,peak_t,cm_t,sharp_t)/(peaktq/hp),0))
		return hp_r,((tq_r*(eq/x))*_Tune.Ratios[gear+1]*fFD*hpScaling)
	end
	
	--Electric Curve
	
	local EHP=_Tune.E_Horsepower/100
	local ETQ=_Tune.E_Torque/100
	local ETrans1=_Tune.E_Trans1/1000
	local ETrans2=_Tune.E_Trans2/1000
	local ELimit=_Tune.E_Redline/1000 
	
	function elecHP(RPM)
		RPM=RPM/1000
		local retVal=1e-9
		if RPM<=ETrans1 then
			retVal=((((RPM/ETrans1)^_Tune.EH_FrontMult)/(1/EHP))*(RPM/ETrans1))+((((RPM/ETrans1)^(1/_Tune.EH_FrontMult))/(1/EHP))*(1-(RPM/ETrans1)))
		elseif ETrans1<RPM and RPM<ETrans2 then
			retVal=EHP
		elseif ETrans2<=RPM then
			retVal=EHP-(((RPM-ETrans2)/(ELimit-ETrans2))^_Tune.EH_EndMult)/(1/(EHP*(_Tune.EH_EndPercent/100)))
		else
			error( "\n\t [NCT: M]: Drive initialization failed!"
			.."\n\t    An unknown error occured when initializing electric horsepower."
			.."\n\t    Please send a screenshot of this message to Avxnturador."
			.."\n\t    R: "..RPM..", T1: "..ETrans1", T2: "..ETrans2", L: "..ELimit".")
		end
		return retVal
	end
	
	function elecTQ(RPM)
		RPM=RPM/1000
		local retVal=1e-9
		if RPM<ETrans1 then
			retVal=ETQ
		elseif ETrans1<=RPM then
			retVal=ETQ-(((RPM-ETrans1)/(ELimit-ETrans1))^_Tune.ET_EndMult)/(1/(ETQ*(_Tune.ET_EndPercent/100)))
		else
			error( "\n\t [NCT: M]: Drive initialization failed!"
			.."\n\t    An unknown error occured when initializing electric torque."
			.."\n\t    Please send a screenshot of this message to Avxnturador."
			.."\n\t    R: "..RPM..", T1: "..ETrans1", T2: "..ETrans2", L: "..ELimit".")
		end
		return retVal
	end
	
	--Plot Current Electric Horsepower
	function ElecCurve(x,gear)
		local hp=(math.max(elecHP(x),0))*100
		local tq=(math.max(elecTQ(x),0))*100
		if gear~=0 then
			return hp,math.max(tq*_Tune.Ratios[gear+1]*fFD*hpScaling,0)
		else
			return 0,0
		end
	end
	
	--Output Cache	
	local NCache = {}
	local ECache = {}
	local TCache = {}
	local SCache = {}
	
	for gear,ratio in pairs(_Tune.Ratios) do
		local nhpPlot = {}
		local ehpPlot = {}
		local thpPlot = {}
		local shpPlot = {}
		for rpm = 0, math.ceil((_Tune.Redline+100)/100) do
			local ntqPlot = {}
			local etqPlot = {}
			local ttqPlot = {}
			local stqPlot = {}
			if rpm~=0 then
				if _Tune.Engine then
					ntqPlot.Horsepower,ntqPlot.Torque = EngineCurve(rpm*100,gear-1,V_MT,_Tune.M_EqPoint,PeakHP_N,PeakTQ_N)
					if _TCount~=0 then
						ttqPlot.Horsepower,ttqPlot.Torque = EngineCurve(rpm*100,gear-1,V_TC,_Tune.M_EqPoint,PeakHP_T,PeakTQ_T)
					else
						ttqPlot.Horsepower,ttqPlot.Torque = 0,0
					end
					if _SCount~=0 then
						stqPlot.Horsepower,stqPlot.Torque = EngineCurve(rpm*100,gear-1,V_SC,_Tune.M_EqPoint,PeakHP_S,PeakTQ_S)
					else
						stqPlot.Horsepower,stqPlot.Torque = 0,0
					end
				else
					ntqPlot.Horsepower,ntqPlot.Torque = 0,0
					ttqPlot.Horsepower,ttqPlot.Torque = 0,0
					stqPlot.Horsepower,stqPlot.Torque = 0,0
				end
				if _Tune.Electric then
					etqPlot.Horsepower,etqPlot.Torque = ElecCurve(rpm*100,gear-1)
				else
					etqPlot.Horsepower,etqPlot.Torque = 0,0
				end
			else
				ntqPlot.Horsepower,ntqPlot.Torque = 0,0
				etqPlot.Horsepower,etqPlot.Torque = 0,0
				ttqPlot.Horsepower,ttqPlot.Torque = 0,0
				stqPlot.Horsepower,stqPlot.Torque = 0,0
			end
			if _Tune.Engine then
				nhp,ntq = EngineCurve((rpm+1)*100,gear-1,V_MT,_Tune.M_EqPoint,PeakHP_N,PeakTQ_N)
				if _TCount~=0 then
					thp,ttq = EngineCurve((rpm+1)*100,gear-1,V_TC,_Tune.M_EqPoint,PeakHP_T,PeakTQ_T)
				else
					thp,ttq = 0,0
				end
				if _SCount~=0 then
					shp,stq = EngineCurve((rpm+1)*100,gear-1,V_SC,_Tune.M_EqPoint,PeakHP_S,PeakTQ_S)
				else
					shp,stq = 0,0
				end
			else
				nhp,ntq = 0,0
				thp,ttq = 0,0
				shp,stq = 0,0
			end
			if _Tune.Electric then
				ehp,etq = ElecCurve((rpm+1)*100,gear-1)
			else
				ehp,etq = 0,0
			end
			ntqPlot.HpSlope,ntqPlot.TqSlope = (nhp-ntqPlot.Horsepower),(ntq-ntqPlot.Torque)
			etqPlot.HpSlope,etqPlot.TqSlope = (ehp-etqPlot.Horsepower),(etq-etqPlot.Torque)
			ttqPlot.HpSlope,ttqPlot.TqSlope = (thp-ttqPlot.Horsepower),(ttq-ttqPlot.Torque)
			stqPlot.HpSlope,stqPlot.TqSlope = (shp-stqPlot.Horsepower),(stq-stqPlot.Torque)
			nhpPlot[rpm] = ntqPlot
			ehpPlot[rpm] = etqPlot
			thpPlot[rpm] = ttqPlot
			shpPlot[rpm] = stqPlot
		end
		table.insert(NCache,nhpPlot)
		table.insert(ECache,ehpPlot)
		table.insert(TCache,thpPlot)
		table.insert(SCache,shpPlot)
	end
	
	--Powertrain
	wait()

	function Auto()
		local maxSpin=0
		if Rear.Wheel.RotVelocity.Magnitude>maxSpin then maxSpin = Rear.Wheel.RotVelocity.Magnitude end
		if _IsOn then
			if _CGear == 0 and not _Tune.NeutralRev then _CGear = 1 _ClPressing = false end
			if _CGear >= 1 then
				if (_CGear==1 and _InBrake > 0 and bike.DriveSeat.Velocity.Magnitude < 10) and _Tune.NeutralRev then
					_CGear = 0 _ClPressing = false
				elseif bike.DriveSeat.Velocity.Magnitude > 10 then
					if _Tune.AutoShiftMode == "RPM" and not _ClutchSlip then
						if _RPM>(_Tune.PeakRPM+_Tune.AutoUpThresh) then
							if not _ShiftUp and not _Shifting then _ShiftUp = true end
						elseif math.max(math.min(maxSpin*_Tune.Ratios[_CGear+1]*fFDr,_Tune.Redline+100),_Tune.IdleRPM)<(_Tune.PeakRPM-_Tune.AutoDownThresh) and _CGear>1 then
							if not _ShiftDn and not _Shifting then _ShiftDn = true end 
						end
					else
						if bike.DriveSeat.Velocity.Magnitude > math.ceil(wDRatio*(_Tune.PeakRPM+_Tune.AutoUpThresh)/_Tune.Ratios[_CGear+1]/fFD) then
							if not _ShiftUp and not _Shifting then _ShiftUp = true end
						elseif bike.DriveSeat.Velocity.Magnitude < math.ceil(wDRatio*(_Tune.PeakRPM-_Tune.AutoDownThresh)/_Tune.Ratios[_CGear+1]/fFD) and _CGear>1 then
							if not _ShiftDn and not _Shifting then _ShiftDn = true end
						end
					end
				end
			else
				if (_InThrot-(_Tune.IdleThrottle/100) > 0 and bike.DriveSeat.Velocity.Magnitude < 10) and _Tune.NeutralRev then
					_CGear = 1 _ClPressing = false
				end
			end
		end 
	end
	
	function Gear()
		local maxSpin=0
		if Rear.Wheel.RotVelocity.Magnitude>maxSpin then maxSpin = Rear.Wheel.RotVelocity.Magnitude end
		if _ShiftUp and not _Shifting then
			local AutoCheck if _TMode~="Manual" then AutoCheck = true end
			if (_TMode == "Manual" and ((not _Tune.QuickShifter and not _ClPressing) or (not _ClPressing and (_Tune.QuickShifter and tick()-_BTick>_Tune.QuickShiftTime)))) or _CGear == #_Tune.Ratios-1 or (_TMode ~= "Manual" and not _IsOn) then _ShiftUp = false return end
			local NextGear = math.min(_CGear+1,#_Tune.Ratios)
			if _TMode~="Manual" then
				_Shifting = true
				if _CGear>0 then 
					if _Tune.AutoShiftType=="DCT" then 
						wait(_Tune.ShiftUpTime)
					elseif _Tune.AutoShiftType=="Rev" then
						repeat wait() until _RPM<=math.max(math.min(maxSpin*_Tune.Ratios[NextGear]*fFDr,_Tune.Redline-_Tune.RevBounce),_Tune.IdleRPM) or not _IsOn or _ShiftDn 
					end
				end
			end
			_ShiftUp = false
			_Shifting = false
			if _TMode ~= "Manual" and not _IsOn then return end
			_CGear = math.min(_CGear+1,#_Tune.Ratios-1)
			if _TMode ~= "Manual" or (_TMode == "Manual" and (_CGear == 1 or AutoCheck)) and _IsOn then _ClPressing = false end
		end
		if _ShiftDn and not _Shifting then
			local AutoCheck if _TMode~="Manual" then AutoCheck = true end
			if (_TMode == "Manual" and ((not _Tune.QuickShifter and not _ClPressing) or (not _ClPressing and (_Tune.QuickShifter and tick()-_TTick>_Tune.QuickShiftTime)))) or _CGear == 0 or (_TMode ~= "Manual" and not _IsOn) then _ShiftDn = false return end
			local PrevGear = math.min(_CGear,#_Tune.Ratios)
			if _TMode~="Manual" then
				_Shifting = true
				if _CGear>1 then 
					if _Tune.AutoShiftType=="DCT" then 
						wait(_Tune.ShiftDnTime)
					elseif _Tune.AutoShiftType=="Rev" then
						repeat wait() until _RPM>=math.max(math.min(maxSpin*_Tune.Ratios[PrevGear]*fFDr,_Tune.Redline-_Tune.RevBounce),_Tune.IdleRPM) or not _IsOn or _ShiftUp
					end
				end
			end
			_ShiftDn = false
			_Shifting = false
			if _TMode ~= "Manual" and not _IsOn then return end
			_CGear = math.max(_CGear-1,0)
			if _TMode ~= "Manual" or (_TMode == "Manual" and (_CGear == 0 or AutoCheck)) and _IsOn then _ClPressing = false end
		end
	end

	local tqTCS = 1
	local sthrot = 0
	local _StallOK = false
	local ticc = tick()
	local bticc = tick()
	--Apply Power
	function Engine(dt)
		local deltaTime = (60/(1/dt))
		--Determine Clutch Slip
		if _TMode~="Manual" and _Tune.SlipClutch then
			if _Tune.ABS and _ABS then
				if _SlipCount >= _Tune.SlipABSThres then _ClutchSlip = true else _ClutchSlip = false end
			else
				if tick()-bticc >= _Tune.SlipTimeThres then _ClutchSlip = true else _ClutchSlip = false end
			end
		else
			_ClutchSlip = false
		end
		
		--Neutral Gear
		if ((_CGear == 0 or _Shifting) and _IsOn) then 
			_ClPressing = true
			_Clutch = 1
			_StallOK = false
		end
		
		local revMin = _Tune.IdleRPM 
		local goalMin = _Tune.IdleRPM 
		if _Tune.Stall and _Tune.Clutch then revMin = 0 end
		
		if _Shifting and _ShiftUp then 
			_GThrot = 0
		elseif _Shifting and _ShiftDn then
			_GThrot = (_Tune.ShiftThrot/100)
		else
			_GThrot = _InThrot
		end
		_GBrake = _InBrake
		
		if not _IsOn then 
			ticc = tick()
			revMin = 0 
			goalMin = 0
			_GThrot = _Tune.IdleThrottle/100
			if _TMode~="Manual" then 
				_CGear = 0 
				_ClPressing = true 
				_Clutch = 1 
			end
		end
		
		--Determine RPM
		local maxSpin=0
		if Rear.Wheel.RotVelocity.Magnitude>maxSpin then maxSpin = Rear.Wheel.RotVelocity.Magnitude end
		local _GoalRPM=0
		
		if _Tune.Engine or _Tune.Electric then
			_GoalRPM = math.max(math.min((_RPM-_Tune.RevDecay*deltaTime)+((_Tune.RevDecay+_Tune.RevAccel)*_GThrot*deltaTime),_Tune.Redline+100),goalMin)
		end
		
		if _GoalRPM>_Tune.Redline then 
			if _CGear<#_Tune.Ratios-2 then
				_GoalRPM = _GoalRPM-_Tune.RevBounce
			else
				_GoalRPM = _GoalRPM-_Tune.RevBounce*.5
			end
		end
		
		local _WheelRPM = maxSpin*_Tune.Ratios[_CGear+1]*fFDr
		
		--Clutch Calculation
		if _Tune.Clutch then
			if script.Parent.Values.AutoClutch.Value and _IsOn then
				if _Tune.ClutchType == "Clutch" then
					if _ClPressing or _ClutchSlip then _ClutchKick = 1 end
					_ClutchKick = _ClutchKick*(_Tune.ClutchEngage/100)		
					local ClRPMInfluence = math.max((_RPM-_Tune.IdleRPM)*_Tune.ClutchRPMMult/(_Tune.Redline-_Tune.IdleRPM),0)
					if _Tune.ClutchMode == "New" then ClRPMInfluence = 0 end
					_ClutchModulate = math.min(((((script.Parent.Values.Velocity.Value.Magnitude/_Tune.SpeedEngage)/math.abs(_CGear)) + ClRPMInfluence) - _ClutchKick), 1)
				elseif _Tune.ClutchType == "CVT" or (_Tune.ClutchType == "TorqueConverter" and _Tune.TQLock) then
					if (_GThrot-(_Tune.IdleThrottle/100)==0 and script.Parent.Values.Velocity.Value.Magnitude<_Tune.SpeedEngage) or (_GThrot-(_Tune.IdleThrottle/100)~=0 and (_RPM < _Tune.RPMEngage and _WheelRPM < _Tune.RPMEngage)) then
						_ClutchModulate = math.min(_ClutchModulate*(_Tune.ClutchEngage/100), 1)
					else
						_ClutchModulate = math.min(_ClutchModulate*(_Tune.ClutchEngage/100)+(1-(_Tune.ClutchEngage/100)), 1)
					end
				elseif _Tune.ClutchType == "TorqueConverter" and not _Tune.TQLock then
					_ClutchModulate = math.min((_RPM/_Tune.Redline)*0.7, 1)
				end
				if not _ClPressing and not _ClutchSlip then _Clutch = math.min(1-_ClutchModulate,1) else _Clutch = 1 end
				_StallOK = (_Clutch<=0.01) or _StallOK
			else
				_StallOK = _Tune.Stall
				_Clutch = script.Parent.Values.Clutch.Value
			end
		else
			_StallOK = false
			if (not _ClPressing and not _ClutchSlip) and not _Shifting then _Clutch = 0 else _Clutch = 1 end
		end
		
		local aRPM = math.max(math.min((_GoalRPM*_Clutch) + (_WheelRPM*(1-_Clutch)),_Tune.Redline+100),revMin)
		local clutchP = math.min(math.abs(aRPM-_RPM)/(_Tune.Flywheel*deltaTime),.9)
		if _ClPressing or _ClutchSlip then clutchP = 0 end
		_RPM = _RPM*clutchP  +  aRPM*(1-clutchP)
		
		if _RPM<=(_Tune.IdleRPM/4) and _StallOK and (tick()-ticc>=0.2) then script.Parent.IsOn.Value = not _Tune.Stall end
		
		--Rev Limiter
		_spLimit = -((_Tune.Redline+100)/(fFDr*_Tune.Ratios[_CGear+1]))
		if _Tune.Limiter then _spLimit = math.min(_spLimit,-_Tune.SpeedLimit) end
		if _RPM>_Tune.Redline then 
			if _CGear<#_Tune.Ratios-2 then
				_RPM = _RPM-_Tune.RevBounce
			else
				_RPM = _RPM-_Tune.RevBounce*.5
			end
		end
		
		--Aspiration
		local TPsi = _TPsi/_TCount
		
		local _BThrot = _GThrot
		
		if _Tune.Engine then
			if _TCount~=0 then
				_TBoost = _TBoost + ((((((_HP*(_BThrot*1.2)/_Tune.Horsepower)/8)-(((_TBoost/TPsi*(TPsi/15)))))*((8/(_Tune.T_Size/(deltaTime)))*2))/TPsi)*15)
				if _TBoost < 0.05 then _TBoost = 0.05 elseif _TBoost > 2 then _TBoost = 2 end
			else
				_TBoost = 0
			end
			if _SCount~=0 then
				if _BThrot>sthrot then
					sthrot=math.min(_BThrot,sthrot+_Tune.S_Sensitivity*deltaTime)
				elseif _BThrot<sthrot then
					sthrot=math.max(_BThrot,sthrot-_Tune.S_Sensitivity*deltaTime)
				end
				_SBoost = (_RPM/_Tune.Redline)*(.5+(1.5*sthrot))
			else
				_SBoost = 0
			end
		else
			_TBoost = 0
			_SBoost = 0
		end
		
		--Torque calculations
		if _Tune.Engine then
			local cTq = NCache[_CGear+1][math.floor(math.min(_Tune.Redline,math.max(0,_RPM))/100)]
			_NH = cTq.Horsepower+(cTq.HpSlope*(((_RPM-math.floor(_RPM/100))/100)%1))
			_NT = cTq.Torque+(cTq.TqSlope*(((_RPM-math.floor(_RPM/100))/100)%1))
			if _TCount~=0 then
				local tTq = TCache[_CGear+1][math.floor(math.min(_Tune.Redline,math.max(0,_RPM))/100)]
				_TH = (tTq.Horsepower+(tTq.HpSlope*(((_RPM-math.floor(_RPM/100))/100)%1)))*(_TBoost/2)
				_TT = (tTq.Torque+(tTq.TqSlope*(((_RPM-math.floor(_RPM/100))/100)%1)))*(_TBoost/2)
			else
				_TH,_TT = 0,0
			end
			if _SCount~=0 then
				local sTq = SCache[_CGear+1][math.floor(math.min(_Tune.Redline,math.max(0,_RPM))/100)]
				_SH = (sTq.Horsepower+(sTq.HpSlope*(((_RPM-math.floor(_RPM/100))/100)%1)))*(_SBoost/2)
				_ST = (sTq.Torque+(sTq.TqSlope*(((_RPM-math.floor(_RPM/100))/100)%1)))*(_SBoost/2)
			else
				_SH,_ST = 0,0
			end
			_BH = _TH+_SH
			_BT = _TT+_ST
		else
			_NH,_NT = 0,0
			_TH,_TT = 0,0
			_SH,_ST = 0,0
			_BH,_BT = 0,0
		end
		
		if _Tune.Electric and _CGear~=0 then
			local eTq = ECache[_CGear+1][math.floor(math.min(_Tune.Redline,math.max(100,_RPM))/100)]
			_EH = eTq.Horsepower+(eTq.HpSlope*(((_RPM-math.floor(_RPM/100))/100)%1))
			_ET = eTq.Torque+(eTq.TqSlope*(((_RPM-math.floor(_RPM/100))/100)%1))
		else
			_EH,_ET = 0,0
		end
		
		_HP = _NH + _BH + _EH
		_OutTorque = _NT + _BT + _ET
		
		--Update Wheels
		
		--Front

		_fABSActive = false
		--Output
		if _PBrake and bike.DriveSeat.Velocity.Magnitude<20 then
			--PBrake
			Front.Axle.HingeConstraint.MotorMaxTorque=PBrakeForce*(60/workspace:GetRealPhysicsFPS())
		else
			if _CGear == 0 and bike.DriveSeat.Velocity.Magnitude <= 10 and _Tune.NeutralRev then
				Front.Axle.HingeConstraint.MotorMaxTorque=FBrakeForce*_GThrot*(60/workspace:GetRealPhysicsFPS())
			else
				--Apply ABS
				local ftqABS = 1
				if _ABS and math.abs(Front.Wheel.RotVelocity.Magnitude*(Front.Wheel.Size.Y/2) - Front.Wheel.Velocity.Magnitude)-_Tune.ABSThreshold>0 then ftqABS = 0 end
				if ftqABS < 1 then _fABSActive = true end
				local brake = FBrakeForce
				if _Tune.LinkedBrakes then
					local bias = _Tune.BrakesRatio/100
					brake = brake*bias
				end
				Front.Axle.HingeConstraint.MotorMaxTorque=brake*_GBrake*ftqABS*(60/workspace:GetRealPhysicsFPS())
			end
		end
		
		--Rear
		
		_TCSActive = false
		_rABSActive = false
		--Output
		if _PBrake and bike.DriveSeat.Velocity.Magnitude>=20 then
			--PBrake
			_SlipCount = _Tune.SlipABSThres
			bticc = tick()-_Tune.SlipTimeThres
			Rear.Axle.HingeConstraint.MotorMaxTorque=PBrakeForce*(60/workspace:GetRealPhysicsFPS())
			Rear.Axle.HingeConstraint.MotorMaxAcceleration=9e9
			Rear.Axle.HingeConstraint.AngularVelocity=0
		else
			--Apply Power
			if _CGear == 0 and bike.DriveSeat.Velocity.Magnitude <= 10 and _Tune.NeutralRev then
				bticc = tick()
				_SlipCount = 0
				Rear.Axle.HingeConstraint.MotorMaxTorque=math.min((500*_GBrake)+(RBrakeForce*_GThrot),500)*(60/workspace:GetRealPhysicsFPS())
				Rear.Axle.HingeConstraint.AngularVelocity=7*_GBrake*(1-_GThrot)
			elseif _GBrake==0 then
				bticc = tick()
				_SlipCount = 0
				local on=1
				if not script.Parent.IsOn.Value then on=0 end
				local throt = _GThrot
				local clutch=1
				if _ClPressing then clutch=0 end
						
				--Apply TCS
				local tqTCS = 1
				if _TCS then tqTCS = 1-(math.min(math.max(0,math.abs(Rear.Wheel.RotVelocity.Magnitude*(wDia/2) - Rear.Wheel.Velocity.Magnitude)-_Tune.TCSThreshold)/_Tune.TCSGradient,1)*(1-(_Tune.TCSLimit/100))) end
				_TCSAmt = tqTCS
				if tqTCS < 1 then
					_TCSAmt = tqTCS
					_TCSActive = true
				end
				
				--Update Forces
				Rear.Axle.HingeConstraint.MotorMaxTorque=_OutTorque*throt*tqTCS*on*clutch*(60/workspace:GetRealPhysicsFPS())*(1+(Rear.Wheel.RotVelocity.Magnitude/(120-workspace:GetRealPhysicsFPS()))^(((1-_Tune.Drag)*1.15)+(((1-_Tune.Drag)*.07)*(1-(60/workspace:GetRealPhysicsFPS())))))
				Rear.Axle.HingeConstraint.MotorMaxAcceleration=_RPM*(math.pi/30)
				Rear.Axle.HingeConstraint.AngularVelocity=_spLimit
			--Brakes
			else
				--Apply ABS
				local rtqABS = 1
				if math.abs(Rear.Wheel.RotVelocity.Magnitude*(wDia/2) - Rear.Wheel.Velocity.Magnitude)-_Tune.ABSThreshold>0 then 
					if _ABS then rtqABS = 0 _SlipCount = _SlipCount + 1 end
				else
					bticc = tick()
				end
				if rtqABS < 1 then _rABSActive = true end
				local brake = RBrakeForce
				if _Tune.LinkedBrakes then
					local bias = _Tune.BrakesRatio/100
					brake = brake*(1-bias)
				end
				
				Rear.Axle.HingeConstraint.MotorMaxTorque=brake*_GBrake*rtqABS*(60/workspace:GetRealPhysicsFPS())
				Rear.Axle.HingeConstraint.MotorMaxAcceleration=9e9
				Rear.Axle.HingeConstraint.AngularVelocity=0
			end
		end
	end