Back port the changes from c2504021b7b3dc7f5daef8e0ffc736c2dd3e285b