Issue #1362

This PR contains a fix for #1362. I have also tidied up a couple of things while I was in there (some dead-ends that I followed, etc.) If you look at the commits in order they should hopefully be clear.