refactored dialog code