Refactoring and clean-up

Refactored and cleaned up ks-widget.php