Riesenie inspirovane https://gist.github.com/maxcountryman/1699708