Backported various bug fixes from 0.7.0-pre