Pull request, as requested

Hi

Dan requested I put in a pull request so that you can merge some of my code into the main branch, so this is it. I haven't worked on this for a long time, so I'd be very happy if you manage to get some value out of my code.

Bob