Exception handling and style refactoring