This is my solution to David's problem. It is slightly different to Tom's but its outcome is the same.
By Muhammed