clean error handling

original clean by @Mistobaan
modified