Error handling

I think this helps with issue #21 and other error behaviour where errors would not be cleared properly.