Inventory code optimized, now more legible

new bugs may have been introduced :/