I nicked the clever bits of the code from @MarcKnoll at

trendblog.net/how-to-cr…