This is maybe better for refactoring task but I'd rather keep all logic separated from code required by modal listeners callback ( the deferred pattern here ) we could create a helper function and use it for each of these callback but keep the pure logic clean.