Applied fix for #162 from the gist https://gist.github.com/tstgruby/3794507