Fix error message

And a little bit of refactoring.