I can imagine a problem but it doesn't sound like what you described. Because you are always adding 1 to the slot ID and then using this as the next empty one then deleting items in the middle won't work so I can see that you need to rework the empty slot ID logic but you said the problem is its always added on the slot of the deleted item ? So is that what you meant that it doesn't find the new empty slot ?
To rework that logic to find the empty one you can rather than add 1 to curslotID, add an event to set curslotID to the first column that has 0 amount... so for each x if no item name at curx,0 then set that x to the curslotID. You can add this to top of the function before the other checks.