code refactoring using common error handling