Tidied up the widget generation code