Sync with 0.3x branch and prepare for 0.40 release