Refactoring exception handling