diff --git a/init.lua b/init.lua
index ebaa32f..37e846a 100644
--- a/init.lua
+++ b/init.lua
@@ -689,7 +689,11 @@ minetest.register_on_generated(function(minp, maxp, seed)
 					stacksz = math.floor(items[ii]:get_count() / 2 ^ 8)
 					if pr:next(0, prob) == 0 then
 						stk = ItemStack({name=items[ii]:get_name(), count=stacksz, wear=items[ii]:get_count(), metadata=items[ii]:get_metadata()})
-						inv:add_item("main", stk)
+						local ind = pr:next(1, inv:get_size("main"))
+						while not inv:get_stack("main",ind):is_empty() do
+							ind = pr:next(1, inv:get_size("main"))
+						end
+						inv:set_stack("main", ind, stk)
 					end
 				end
 			end