This is a copy of https://gist.github.com/sinistersnare/6367829 .