Import from https://gist.github.com/nyarla/7624599