Correct Error handling
Add some unit tests for errors