refactor dialog code