slightly improved error handling