Partially merge Andreas' pull request.