quickly resolved problem 3 and optimized with a bit of help