More robust error checking and added an error dialog box.