Quite a lot of cleaning up and, as a result, improved code.