(messily) solved problem 3