Updated JavaScript to use function