Backported fixes, changes from TESTING branch.