refactoring err handle