better error handling (part of #250)