;(function (m) {
	'use strict';

	chrome.storage.local.get(function (storage) {

		var tabs = {}, // to-be module
			tabGroups = storage.tabGroups || [], // tab groups
			opts = storage.options || {
				deleteTabOnOpen: 'no',
				closePinnedTabs: 'no'
			};

		function saveTabGroups(json) {
			chrome.storage.local.set({ tabGroups: json });
		}

		// model entity
		// 'data' is meant to be a tab group object from localStorage
		tabs.TabGroup = function (data) {
			this.date = m.prop(data.date);
			this.id   = m.prop(data.id);
			this.tabs = m.prop(data.tabs);
		};

		// alias for Array
		tabs.TabGroupsList = Array;

		// view-model
		tabs.vm = new function () {
			var vm = {};
			vm.init = function () {
				// list of tab groups
				vm.list = new tabs.TabGroupsList();

				vm.rmGroup = function (i) {
					// remove view from array
					vm.list.splice(i, 1);
					// remove from localStorage
					tabGroups.splice(i, 1);
					// save
					saveTabGroups(tabGroups);
				};

				vm.rmTab = function (i, ii) {
					// remove from view array
					//vm.list[i].tabs().splice(ii, 1);
					// remove from localStorage
					tabGroups[i].tabs.splice(ii, 1);
					// save
					saveTabGroups(tabGroups);
				};
			};
			return vm;
		};

		tabs.controller = function () {
			var i;
				tabs.vm.init();
			for (i = 0; i < tabGroups.length; i += 1) {
				tabs.vm.list.push(new tabs.TabGroup(tabGroups[i]));
			}
		};

		tabs.view = function () {
			if (tabs.vm.list.length === 0) {
				return m('p', 'No tabs');
			}

			// foreach tab group
			return tabs.vm.list.map(function (group, i) {
				// group
				return m('div.group', [
					m('div.group-title', [
						m('span.delete-link', { onclick: function () {
							tabs.vm.rmGroup(i);
						} }),
						m('span.group-date', { title: moment(group.date()).format('MMMM Do YYYY, H:mm') }, moment(group.date()).fromNow() ),
						' ',
						m('span.group-amount', group.tabs().length + ' tab' + (group.tabs().length===1?'':'s')),
						' ',
						m('span.restore-all.button.button-small', { onclick: function () {
							var i;

							// reason this goes before opening the tabs and not
							// after is because it doesn't work otherwise
							// I imagine it's because you changed tab and so
							// that messes with the focus of the JS somehow...
							if (opts.deleteTabOnOpen === 'yes') {
								tabs.vm.rmGroup(i);
							}

							for (i = 0; i < group.tabs().length; i += 1) {
								chrome.tabs.create({
									url: group.tabs()[i].url,
									pinned: group.tabs()[i].pinned
								});
							}
						} }, 'Restore all')
					]),

					// foreach tab
					m('ul.tab-groups', group.tabs().map(function (tab, ii) {
						return m('li', [
							m('span.delete-link', { onclick: function () {
								tabs.vm.rmTab(i, ii);
							} }),
							m('img', { src: "chrome://favicon/" + tab.url, height: '16', width: '16' }),
							' ',
							m('span.link', { onclick: function () {
								if (opts.deleteTabOnOpen === 'yes') {
									tabs.vm.rmTab(i, ii);
								}

								chrome.tabs.create({
									url: tab.url,
									pinned: tab.pinned
								});
							},
							title: tab.url }, tab.title)
						]);
					}))
				]);
			});
		};

		// init the app
		m.module(document.getElementById('groups'), { controller: tabs.controller, view: tabs.view });

	});

}(m));

@@ -1034,41 +1034,43 @@
 rom 
-view array%0A%09%09%09%09%09//vm.list
+localStorage%0A%09%09%09%09%09tabGroups
 %5Bi%5D.tabs
 ().s
@@ -1065,18 +1065,16 @@
 %5Bi%5D.tabs
-()
 .splice(
@@ -1093,38 +1093,44 @@
 %09// 
-r
+R
 emove 
-from localStorage
+the group if empty.
 %0A%09%09%09%09%09
+if (
 tabG
@@ -1139,38 +1139,152 @@
 ups%5Bi%5D.tabs.
-splice(ii, 1);
+length === 0) %7B%0A%09%09%09%09%09%09vm.rmGroup(i);%0A%09%09%09%09%09%09// We fall-through here even if rmGroup%0A%09%09%09%09%09%09// already saves the tab groups.%0A%09%09%09%09%09%7D
 %0A%09%09%09%09%09// sav

Remove empty tab groups