Daniel Thompson has loads of useful code out there :) Reflects 07eff5c0b7333cfcc3d98ddc6ebf26e34c9c02ef of that repo.