Refactor code: move checks and dialog handling into more general function.