Carrying over changes from Monzur's 4dfb412b697807a2b9726a78ba018230fdc22ced.