apply https://gist.github.com/bartv2/6038598