Copy of https://gist.github.com/mnot/246089, version 0.93