Refactors widget code