refactored problem 5 and solved problem 6