Ported changes from master branch to 0.9-dev