Refactor and unify error printing