Fixed exception handling and added tests to complete test coverage.