Backported the upstream patch instead. Less error prone. Signed-off-by: Rosen Penev <rosenp@gmail.com>