refactor errors and tests