initial import from GIST

https://gist.github.com/matbor/5931466