refactoring and improvements for the inventory management

added an intelligent swap