Accidently regressed out changes in last commit

The merge for 5.0.4 regressed out some of the changes for #302 -
re-introduced them here.