better error messages, some refactoring