solution of problem 4 and some minor changes