Updated JavaScript logic