Added more tests and refactored exception handling.