A fork from: https://gist.github.com/JesseCrocker/8407623