Added solutions for problem 4, and did some slight refactoring