Tweak exception handling, add tests