close dialog correctly