refactoring and adding console statements for debugging task error