Fix exceptions and add tests