error treatment refactorized