Added another implementation copied from a gist referenced in the comment