My solution for https://gist.github.com/bingwen/b5d8853aded60df316ca