Inventory fixes

You can read the commit names for a description.
I still have no idea what this piece of code does, not why it's here. Your comment should explain the reason for the code.