Refactoring and fixes for ModalDialog