Improved js code for the widget.