added error dialogs and refactored some code