manually duplicating #650 in the master branch