refactoring exception and error handling