Initial commit based on https://gist.github.com/neonstalwart/6630466