First working version of problem 1