Fork of https://gist.github.com/nbremer/6052681