Working solution to problem 3.
Base code for exam q4