git repo version of https://gist.github.com/sztupy/9138124