Solved problem 4. Added tests.