Inspired by this Gist https://gist.github.com/josevalim/1942658