This commit applies @smaftoul patch from https://gist.github.com/smaftoul/2d6fcf43b7c6545aa588/revisions