Refactoring to eliminate duplicate code.
Exception handling improvements.