Added stuff from Gist

Taken from https://gist.github.com/Half-Shot/6815246