Added more error handling and tests.