Inventory fixes

You can read the commit names for a description.
I'd appreciate a comment above this if, explaining what is going on in this block.