Backported some of the changes from the 0.2.0 branch.