From https://gist.github.com/jbenet/ee6c9ac48068889b0912