diff --git a/sim/gate.lua b/sim/gate.lua
index a77ecb3..56a8537 100644
--- a/sim/gate.lua
+++ b/sim/gate.lua
@@ -93,7 +93,7 @@ function Gate.queue(gate, delay)
 end
 
 function Gate.gettick(gate)
-	return GSim.current_tick
+	return GSim.current_tick[0]
 end
 
 function Gate.getdefinition(gate)
diff --git a/sim/port.lua b/sim/port.lua
index b6d1421..1989f4c 100644
--- a/sim/port.lua
+++ b/sim/port.lua
@@ -36,11 +36,11 @@ function Port.getconnectionposition(port)
 end
 
 function Port.isrising(port)
-	return port.group.state[0]==1 and (port.group.update_tick[0] == GSim.current_tick)
+	return port.group.state[0]==1 and (port.group.update_tick[0] == GSim.current_tick[0])
 end
 
 function Port.isfalling(port)
-	return port.group.state[0]==0 and (port.group.update_tick[0] == GSim.current_tick)
+	return port.group.state[0]==0 and (port.group.update_tick[0] == GSim.current_tick[0])
 end
 
 function Port.getgate(port)
diff --git a/sim/simulation.lua b/sim/simulation.lua
index e38e6d4..0c5f989 100644
--- a/sim/simulation.lua
+++ b/sim/simulation.lua
@@ -8,14 +8,17 @@ ffi.cdef [[
 
 Simulation = {}
 
+local queue_max = 65536
+
 function Simulation.new(sim)
 	local o = {
-		groupqueue = ffi.new("struct Net*[131072]"),
+		-- Logic Critical
+		groupqueue = ffi.new("struct Net*["..queue_max.."]"),
 		num_groupqueue = ffi.new("int[1]"),
-		gatequeue = ffi.new("struct Gate*[131072]"),
+		gatequeue = ffi.new("struct Gate*["..queue_max.."]"),
 		num_gatequeue = ffi.new("int[1]"),
+		current_tick = ffi.new("int[1]"),
 		--groupfxqueue = {},
-		current_tick = 0,
 		
 		definitions = {},
 		wires = {},
@@ -30,12 +33,12 @@ function Simulation.new(sim)
 		inputqueue = nil,
 		tickqueue = {},
 		callbacks = nil,
-		
 	}
 	setmetatable(o, sim)
 	sim.__index = sim
 	o.num_groupqueue[0] = 0
 	o.num_gatequeue[0] = 0
+	o.current_tick[0] = 0
 	return o
 end
 
@@ -256,6 +259,7 @@ end
 
 -- Logic Critical
 function Simulation.queuegate_c(sim, cgate)
+	assert(sim.num_gatequeue[0] < queue_max-1)
 	sim.gatequeue[sim.num_gatequeue[0]] = cgate
 	sim.num_gatequeue[0] = sim.num_gatequeue[0] + 1
 	cgate.in_queue[0] = 1
@@ -274,7 +278,7 @@ end
 
 -- Logic Critical
 function Simulation.queuegatelater(sim, gate, delay)
-	local tick = sim.current_tick + delay
+	local tick = sim.current_tick[0] + delay
 	if sim.tickqueue[tick] == nil then
 		sim.tickqueue[tick] = {}
 	end
@@ -294,6 +298,7 @@ end
 
 -- Logic Critical
 function Simulation.queuegroup_c(sim, cnet)
+	assert(sim.num_groupqueue[0] < queue_max-1)
 	sim.groupqueue[sim.num_groupqueue[0]] = cnet
 	sim.num_groupqueue[0] = sim.num_groupqueue[0] + 1
 	cnet.in_queue[0] = 1
@@ -343,19 +348,19 @@ end
 function Simulation.ticklogic(sim)
 	for i = 0, sim.num_groupqueue[0]-1 do
 		local cnet = sim.groupqueue[i]
-		Group.update_c(cnet, sim.current_tick)
+		Group.update_c(cnet, sim.current_tick[0])
 		cnet.in_queue[0] = 0
 		sim.groupqueue[i] = nil
 	end
 	sim.num_groupqueue[0] = 0
 	
-	if sim.tickqueue[sim.current_tick] ~= nil then
-		for i, gate in pairs(sim.tickqueue[sim.current_tick]) do
+	if sim.tickqueue[sim.current_tick[0]] ~= nil then
+		for i, gate in pairs(sim.tickqueue[sim.current_tick[0]]) do
 			if gate.in_queue[0]==0 then
 				Simulation.queuegate(sim, gate)
 			end
 		end
-		sim.tickqueue[sim.current_tick] = nil
+		sim.tickqueue[sim.current_tick[0]] = nil
 	end
 	
 	for i = 0, sim.num_gatequeue[0]-1 do
@@ -367,7 +372,7 @@ function Simulation.ticklogic(sim)
 	end
 	sim.num_gatequeue[0] = 0
 	
-	sim.current_tick = sim.current_tick + 1
+	sim.current_tick[0] = sim.current_tick[0] + 1
 end
 
 function Simulation.tickinit(sim)