Solution for exercise at https://gist.github.com/diusricky/2913880