refactored problem 13.