restructure code and redesign the dialog