integrate some contributions

See gist from jonathan.stott@gmail.com at:
https://gist.github.com/namelessjon/9651948