Made code more efficient and bug-proof

Made the code more efficient in terms of performance by only getting variables when necessary. Also, made sure the inventory does not bug for the player as removing items in a loop can cause inventories to glitch sometimes.