Fix #618

@garyb @joneshf I think this is good to go, other than adding a few more tests. Could you please review?
No, not any more. I originally changed the return statement to not use `d` any more, so I removed it, but now it can be `d` again.