Working version of problem 6, correct solution is achieved