I don't really understand why this works.

https://gist.github.com/thomas-holmes/cb589b10239757d399c8