Changed behavior for errors; some refactoring