Refactor OK button click handler. Fix random crashes.