Initial version with minor changes from haku's gist.