updated from https://gist.github.com/dr-fozzy/9638661