(somewhat better) solution to problem 5 added