Some re-organisation, commenting, and bug fixes