My solution to https://gist.github.com/ctcherry/ee2b56739a559edf05f2