Forked gist to git (originally from https://gist.github.com/dankrause/5585907)