Moved dialog box code to the bottom