Much better and apparently simpler version too.