fixed clumsy error handling