re-revert accidentally reverted changes

Sometimes cherry-picking code isn't a bad idea. ;-)
This should fix all the changes that have been reverted by the followed merged pull request.