refactored JS for widget