Refactoring for error and print