Added David McLaren's accepted solution.