refactored problem 13