Fix error reporting, add tests