Bugfix, better test coverage

Also a bit better error handling