This code is borrowed from another open PR, but I wanted a new commit ID to avoid problems with the commit statuses.