Added duplicated items check and optimized JS code