refactored problem 4 of hw3