Made this as an exercise a while back:

https://news.ycombinator.com/item?id=35528155