Solution for the gist https://gist.github.com/awilliams/696d92ef62edaf45e840