refactor error reporting etc
more refactoring