rewrote ex6 solution, now seems to work, albeit roughly