Change error handling

Also much better tests