refactored exception handling