Refactored Exception handling code