Fixed exceptions and added tests.